我想在android中使用sharedpreferrences。我把数据放到它但是当在其他活动中获取数据时,它找不到prefs.getboolean。
class A{
SharedPreferences.Editor editor = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0).edit();
editor.putBoolean("BotKey", true);
editor.commit();}
class B{
SharedPreferences prefs = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0);
SharedPreferences.Editor editor = prefs.edit();
if(prefs.getBoolean("BotKey",false)){}
}
答案 0 :(得分:0)
尝试使用这种方式:
将值存储在共享首选项中:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("BotKey", true);
editor.apply();
从共享首选项中检索值:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isBotKey = preferences.getBoolean("BotKey", false);