package
com.terry;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.Properties;
import
android.app.Activity;
import
android.content.Context;
import
android.content.SharedPreferences;
import
android.os.Bundle;
import
android.view.KeyEvent;
import
android.widget.CheckBox;
import
android.widget.CompoundButton;
import
android.widget.TextView;
import
android.widget.CompoundButton.OnCheckedChangeListener;
public
class
sharedPreActivity
extends
Activity {
private
TextView myTextView;
private
CheckBox myBox;
private
playMusic PLAYER
=
null
;
private
boolean
isplay
=
false
;
/**
Called when the activity is first created.
*/
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView
=
(TextView)findViewById(R.id.TextView01);
myBox
=
(CheckBox)findViewById(R.id.CheckBox01);
PLAYER
=
new
playMusic(
this
);
/*
* 文件创建模式:Activity.MODE_APPEND
* 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
*/
/*
* 文件创建模式:MODE_PRIVATE
* 默认模式,在那里创建的文件只能由应用程序调用,即为私有的
*/
/*
* 文件创建模式:Activity.MODE_WORLD_READABLE
* 允许所有其他应用程序有读取和创建文件的权限。
*/
/*
* 文件创建模式:Activity.MODE_WORLD_WRITEABLE
* 允许所有其他应用程序具有写入、访问和创建的文件权限。
*/
/*
* SharedPreferences---数据存储之获取
SharedPreferences settings=getPreferences(Activity.MODE_PRIVATE);
isplay=settings.getBoolean("isplay", false); //通过key值找到value,如果不存在即返回false
*/
load();
myBox.setChecked(isplay);
if
(isplay){
myTextView.setText(
"
当前音乐的播放状态:开
"
);
isplay
=
true
;
PLAYER.Play();
}
else
{
myTextView.setText(
"
当前音乐的播放状态:关
"
);
}
myBox.setOnCheckedChangeListener(
new
OnCheckedChangeListener() {
@Override
public
void
onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
//
TODO Auto-generated method stub
if
(isChecked)
{
myTextView.setText(
"
当前音乐的播放状态:开
"
);
isplay
=
true
;
PLAYER.Play();
}
else
{
myTextView.setText(
"
当前音乐的播放状态:关
"
);
isplay
=
false
;
PLAYER.FreeMusc();
}
}
});
}
@Override
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
//
TODO Auto-generated method stub
if
(keyCode
==
KeyEvent.KEYCODE_BACK){
/*
* SharedPreferences --数据存储之保存
SharedPreferences uiState=getPreferences(0);
SharedPreferences.Editor editor=uiState.edit();
editor.putBoolean("isplay", isplay);
editor.commit();
*/
save();
if
(isplay)
{
PLAYER.FreeMusc();
}
this
.finish();
return
true
;
}
return
super
.onKeyDown(keyCode, event);
}
void
load()
{
Properties properties
=
new
Properties();
try
{
FileInputStream stream
=
this
.openFileInput(
"
music.cfg
"
);
properties.load(stream);
}
catch
(FileNotFoundException e) {
//
TODO: handle exception
return
;
}
catch
(IOException e) {
//
TODO Auto-generated catch block
return
;
}
isplay
=
Boolean.valueOf(properties.get(
"
isplay
"
).toString());
}
boolean
save()
{
Properties properties
=
new
Properties();
properties.put(
"
isplay
"
, String.valueOf(isplay));
try
{
FileOutputStream stream
=
this
.openFileOutput(
"
music.cfg
"
, Context.MODE_WORLD_WRITEABLE);
properties.store(stream,
""
);
}
catch
(FileNotFoundException e) {
//
TODO: handle exception
return
false
;
}
catch
(IOException e) {
//
TODO: handle exception
return
false
;
}
return
true
;
}
}