發現到如果要在其他Service同步偵測,發現在PreferenceActivity所設計的值會有錯誤或抓不到
搜尋後找到了這個方法
public class BackgroundService extends Service { SharedPreferences.OnSharedPreferenceChangeListener listener; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onStart(Intent intent, int startId) { EnableSharedPreferences(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } private void EnableSharedPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { } }; prefs.registerOnSharedPreferenceChangeListener(listener); } }
這樣當在PreferenceActivity做了改變,這邊就會偵測到
參考資料:
http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently
沒有留言:
張貼留言