应用程序重启时,android sharedpreferences不会保存数据

时间:2016-06-24 11:39:14

标签: android sharedpreferences

我有一个sharedpreference可以保存数据,直到应用关闭。当应用重新启动sharedpreferences中的数据时,请告诉我解决方案。我希望当应用关闭或重新启动或手机重启时,preferences中的数据已保存。

public static final String MyPREF_MOB = "MyPref_mob";
public static final String WALLPAPER_MOB = "wallpaper_mob";

if(encodedImagee!=null) {
    // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor edit = shre1.edit();
    edit.putString(WALLPAPER_MOB, encodedImagee);
    edit.apply();
    //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
}
shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);
final String image_save =shre1.getString(WALLPAPER_MOB, "");

我也试过这个

 if(encodedImagee!=null)
        {
           // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor edit = shre1.edit();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.commit();
            //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
        }
        shre1 = PreferenceManager.getDefaultSharedPreferences(context);
        final String image_save =shre1.getString("mob_wallpaper", "");

我已经使用此代码数据保存,当应用程序重启时我也可以获取数据,但当手机重启数据再次消失时

if(encodedImagee!=null)
        {
           // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor edit = shre1.edit();
            edit.remove("mob_wallpaper");
            edit.apply();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.apply();
            //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
        }
        shre1 = PreferenceManager.getDefaultSharedPreferences(context);
        final String image_save =shre1.getString("mob_wallpaper", "");

4 个答案:

答案 0 :(得分:2)

试试这个,

        SharedPreferences sharedPreferences = getSharedPreferences("preference_name", Context.MODE_PRIVATE);
        if (encodedImagee != null){
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.commit();
        }else{
            final String image_save = sharedPreferences.getString("mob_wallpaper","");
            // put your logic over here
        }

答案 1 :(得分:1)

我假设您使用的示例代码

  • PreferenceManager.getDefaultSharedPreferences阅读
  • getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE)撰写

我认为你应该在两种情况下使用相同的东西。

尝试替换

shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);

通过

shre1 = PreferenceManager.getDefaultSharedPreferences(context);

答案 2 :(得分:0)

//get the preference instance with this
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// acccess a saved key value
mPrefs.getString("key", "default_value");
//save a key-value
mPrefs.edit().putString("key", "new_value").apply();

答案 3 :(得分:0)

以这种方式修改您的代码:

@Override
public void onCreate(.....){
    super.onCreate(......);
setContentView(R.layout.your_layout_xml);
shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);

final String image_save =shre1.getString(WALLPAPER_MOB, "");

以及何时想要更新图片

if(encodedImagee!=null) {
// shre1 = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = shre1.edit();
edit.putString(WALLPAPER_MOB, encodedImagee);
edit.commit(); //make use of commit
//  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
}