【发布时间】:2016-01-29 21:53:22
【问题描述】:
我在弹出窗口中有四个按钮(ImageView),每个按钮都会将我的服务类中按钮的颜色更改为所需的颜色。当我单击其中一个按钮时,它会改变颜色,但是当我退出应用程序并重新启动它时,我的选项没有保存。
//Inner class inside my MainActivity
public static class SetReng {
static int reng;
public SetReng() {
}
public int getReng() {
return this.reng;
}
}
public void onGreen (View view) {
SetReng.reng = Color.GREEN;
ImageView Green = (ImageView)view.findViewById(R.id.kesk);
Green.setPressed(true);
Toast.makeText(MainActivity.this, "Bloq color set to green", Toast.LENGTH_SHORT).show();
editor.putInt("Which", SetReng.reng);
editor.apply();
}
这是我的服务中应该加载颜色选项的代码
SetReng putReng = new SetReng();
int theReng= putReng.getReng();
SharedP= PreferenceManager.getDefaultSharedPreferences(context);
int colorOp= SharedP.getInt("Which", theReng);
mButtondeh= new Button(this);
mButtondeh.setBackgroundColor(colorOp);
我做错了什么?
在过去的几天里,我一直坚持这一点,所以任何事情都会不胜感激!
【问题讨论】:
-
您尝试过记录或调试吗?
-
我刚试过;将值保存到共享首选项会进行检查,但不会检查服务类中的值。这是什么意思?
-
保存和加载时必须使用不同的上下文。
-
也许吧,因为在我的 MainActivity 中上下文是 getPreferences(Context.Mode_Private) 但在我的服务中它是 PreferenceManager.getDefaultSharedPreferences(context);你认为这就是原因吗?如果是,有什么解决办法?
-
在这两种情况下都使用 PreferenceManager;另一个是特定于活动的。
标签: android sharedpreferences settings user-input