|
用SharedPreferences存储数据时,在Eclipse->DDMS->File Explorer->/data/data对应项目文件夹下的Shared_prefs文件夹中查找可以保存相关数据,但是在下一次启动时,上一次退出之前保存的数据却不能显示,显示的仍然是保存之前的数据,下面是Activity代码,请问各位,下次启动时如何读取SharedPreferences上次退出时保存的数据?
java 代码
package com.Android.musicplayme;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class PlaySetting extends Activity
{
private TextView set_textView;
private RadioButton sigle_Play, order_Play, random_Play;
private ToggleButton lyLrc;//显示歌词
private ToggleButton dlLrc;//下载歌词
private Button set_bt;//确定按钮
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//设置显示setting.xml布局
setContentView(R.layout.setting);
set_textView = (TextView) findViewById(R.id.setting);
set_textView.setTextColor(Color.WHITE);
sigle_Play = (RadioButton) findViewById(R.id.sigle_play);
order_Play = (RadioButton) findViewById(R.id.order_play);
random_Play = (RadioButton) findViewById(R.id.random_play);
lyLrc = (ToggleButton) findViewById(R.id.ly_lrc);
dlLrc = (ToggleButton) findViewById(R.id.download_lrc);
set_bt = (Button) findViewById(R.id.make);
//设置按钮所显示的文字
set_bt.setText("确定");
//设置按钮宽度
set_bt.setWidth(80);
//设置文字的颜色
set_bt.setTextColor(Color.RED);
//设置文字的大小尺寸
set_bt.setTextSize(18);
//设置按钮的事件监听
set_bt.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
SharedPreferences sp = getSharedPreferences("SET_MSG",
MODE_WORLD_WRITEABLE);// 文件共享
SharedPreferences.Editor editor = sp.edit();
if (sigle_Play.isChecked())
{
editor.putString("sigle_Play", "is_Sigle");
editor.putString("order_Play", null);
editor.putString("random_Play", null);
}
if (order_Play.isChecked()) {
editor.putString("sigle_Play", null);
editor.putString("order_Play", "is_Order");
editor.putString("random_Play", null);
}
if (random_Play.isChecked()) {
editor.putString("sigle_Play", null);
editor.putString("order_Play", null);
editor.putString("random_Play", "is_Random");
}
if (lyLrc.isChecked()) {
editor.putString("lyLrc", "is_Show");
}
if (!lyLrc.isChecked()) {
editor.putString("lyLrc", null);
}
if (dlLrc.isChecked())
{
editor.putString("dlLrc", "is_DownLoad");
}
if (!dlLrc.isChecked())
{
editor.putString("dlLrc", null);
}
editor.commit();// 提交
//Intent intent = new Intent(PlaySetting.this, MusicPlayme.class);
//startActivity(intent);
finish();
}
});
}
} |
|