我无法在 BroadcastReceiver 和 AppCompatActivity 之间共享 SharedPreferences
I can't share SharedPreferences between BroadcastReceiver and AppCompatActivity
我在 BroadcastReceiver 上设置了首选项,但我无法在 AppCompatActivity 上读取。
我确实读了一些 post 但我无法做到这一点。
activity 和 broadcastreceiver 在同一个应用程序上(相同的包名)
在广播接收器上,我在方法 onReceive 上有这段代码:
SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PROPERTIES.SHARENAME,
context.MODE_PRIVATE);
prefs.edit().putBoolean("runnig", true).commit();
在 Activity 我有:
SharedPreferences prefs1 =getApplicationContext().getSharedPreferences(PROPERTIES.SHARENAME,
MODE_PRIVATE);
boolean corriendo= prefs1.getBoolean("running",false);
但是变量 corriendo 仍然是 false。
我尝试在广播接收器上使用 context.getShared...
非常感谢!!
看起来你打错了:
比较:
prefs.edit().putBoolean("runnig", true).commit();
boolean corriendo= prefs1.getBoolean("running",false);
现在只是相关位:
putBoolean("runnig", true)
getBoolean("running",false)
TLDR;您正在读取和写入不同的键。
我在 BroadcastReceiver 上设置了首选项,但我无法在 AppCompatActivity 上读取。 我确实读了一些 post 但我无法做到这一点。 activity 和 broadcastreceiver 在同一个应用程序上(相同的包名) 在广播接收器上,我在方法 onReceive 上有这段代码:
SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PROPERTIES.SHARENAME,
context.MODE_PRIVATE);
prefs.edit().putBoolean("runnig", true).commit();
在 Activity 我有:
SharedPreferences prefs1 =getApplicationContext().getSharedPreferences(PROPERTIES.SHARENAME,
MODE_PRIVATE);
boolean corriendo= prefs1.getBoolean("running",false);
但是变量 corriendo 仍然是 false。
我尝试在广播接收器上使用 context.getShared... 非常感谢!!
看起来你打错了:
比较:
prefs.edit().putBoolean("runnig", true).commit();
boolean corriendo= prefs1.getBoolean("running",false);
现在只是相关位:
putBoolean("runnig", true)
getBoolean("running",false)
TLDR;您正在读取和写入不同的键。