问题描述
我有一个扩展preferenceActivity,并显示我的应用程序的preference屏幕的类。是否可以检查是否有任何变化到preferences做?
这可以帮助...
<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.OnShared$p$pferenceChangeListener.html">http://developer.android.com/reference/android/content/Shared$p$pferences.OnShared$p$pferenceChangeListener.html
其他相关岗位:<一href="http://stackoverflow.com/questions/2542938/shared$p$pferences-onshared$p$pferencechangelistener-not-being-called-consistently">Shared$p$pferences.onShared$p$pferenceChangeListener没有所谓的一贯
公共类preferenceClass扩展preferenceActivity {
OnShared preferenceChangeListener监听;
公共无效的onCreate(包savedInstanceState){
共享preferences preFS = this.getShared preferences(设置,0);
监听=新的共享preferences.OnShared preferenceChangeListener(){
公共无效onShared preferenceChanged(共享preferences preFS,字符串键){
INT标志= 1;
}
};
prefs.registerOnShared preferenceChangeListener(听众);
super.onCreate(空);
加preferencesFromResource(R.xml.settings);
}
}
不要
共享preferences.OnShared preferenceChangeListener spChanged =新
共享preferences.OnShared preferenceChangeListener(){
@覆盖
公共无效onShared preferenceChanged(共享preferences共享preferences,
字符串键){
//你这里的东西
}
};
在你的 preferenceActivity
,也就是让你的 preferenceActivity
类的成员,然后做 registerOnShared preferenceChangeListener(spChanged)
在 preferenceActivity.onCreate()
方法。
这是我做的,我永远不会有问题。
否则它在侦听器有过错的条件检查。发表在code。
编辑:
从C你贴,你应该做的$ C $ preFS
A类成员变量所以它有一个全局范围。
和做 preFS = preferenceManager.getDefaultShared preferences(getApplicationContext());
而不是 getShared preferences
因为你还没有创建该文件。
要创建你需要使用 preferenceManager
的文件。要获得 preferenceManager
,使用 Activity.get preferenceManager()
。
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);
}
}
Do
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// your stuff here
}
};
In your PreferenceActivity
, ie make it a member of your PreferenceActivity
class and then do registerOnSharedPreferenceChangeListener(spChanged)
in the PreferenceActivity.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 of getSharedPreferences
because you haven't created that file.
To create a file you need to use PreferenceManager
. To get a PreferenceManager
, use Activity.getPreferenceManager()
.
这篇关于如何检测是否变化在preferences做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!