我有一个包含两个包的应用程序。在 com.soul.app1.plugin 包中有一个 FireReceiver.class(这是 Tasker 插件的一部分)。值存储得很好,但仅在我第一次运行它时(在重新安装后)才能正确检索(在第二个包 - Settings.class 中)。当我再次尝试检索它时,将检索旧值而不是新值。
com.soul.app1 包中的 Settings.java:
public class Settings extends Activity {
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getApplicationContext().getSharedPreferences("SETTINGS", 0);
int minVolSet = settings.getInt("value", 0);
}
}
com.soul.app1.plugin 包中的 FireReceiver.java:
public final class FireReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, final Intent intent)
{
SharedPreferences.Editor editor;
SharedPreferences settings;
settings = context.getApplicationContext().getSharedPreferences("SETTINGS", 0);
editor = settings.edit();
editor.putInt("value", 2); // constant 2 is for testing purposes only
editor.apply();
}
}