如何检测首选项是否发生更改?
我有一个类扩展 PreferenceActivity 并显示我的应用程序的首选项屏幕。是否可以检查首选项是否有任何更改?
这有助于...
其他相关帖子: SharedPreferences.onSharedPreferenceChangeListener 未被一致调用
public class PreferenceClass extends PreferenceActivity {
OnSharedPreferenceChangeListener listener;
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = this.getSharedPreferences("settings", 0);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
int flag = 1;
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
super.onCreate(null);
addPreferencesFromResource(R.xml.settings);
}
}
I have a class that extends PreferenceActivity and shows the preference screen of my app. Is it possible to check if any changes were made to the preferences?
This helps...
Other related post:
SharedPreferences.onSharedPreferenceChangeListener not being called consistently
public class PreferenceClass extends PreferenceActivity {
OnSharedPreferenceChangeListener listener;
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = this.getSharedPreferences("settings", 0);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
int flag = 1;
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
super.onCreate(null);
addPreferencesFromResource(R.xml.settings);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
PreferenceActivity
中执行操作,即使其成为您的
PreferenceActivity
类的成员,然后在PreferenceActivity.onCreate 中执行
方法。registerOnSharedPreferenceChangeListener(spChanged)
()这就是我所做的,我从来没有遇到过问题。
否则,这是您在侦听器中进行的条件检查出了问题。发布代码。
编辑:
从您发布的代码中,您应该将
prefs
设为类成员变量,以便它具有全局范围。并执行
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
而不是getSharedPreferences
因为您尚未创建该文件。要创建文件,您需要使用
PreferenceManager
。要获取PreferenceManager
,请使用Activity.getPreferenceManager()
。Do
In your
PreferenceActivity
, ie make it a member of yourPreferenceActivity
class and then doregisterOnSharedPreferenceChangeListener(spChanged)
in thePreferenceActivity.onCreate()
method.That's what I do and I never have a problem.
Else it's your conditional checking in the listener that is at fault. Post the code.
EDIT:
From the code you posted, you should make
prefs
a class member variable so it has a global scope.And do
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
instead ofgetSharedPreferences
because you haven't created that file.To create a file you need to use
PreferenceManager
. To get aPreferenceManager
, useActivity.getPreferenceManager()
.在
PreferenceActivity
类中,实现SharedPreferences.OnSharedPreferenceChangeListener
接口。将所需的onSharedPreferenceChanged
方法添加到您的类中,并在onCreate
中注册它。请参阅此处的示例代码:
In your
PreferenceActivity
class, implement theSharedPreferences.OnSharedPreferenceChangeListener
interface. Add the requiredonSharedPreferenceChanged
method to your class and register it in theonCreate
.See sample code here: