OnSharedPreferenceChangeListener and editor.clear()
Okay, I just asked a question earlier tonight regarding giving users a button to reset the preferences of a Live Wallpaper back to their defaults. I figured it out, but pressing the button doesn't actually call OnSharedPref开发者_如何学CerenceChanged until the preference screen has been closed and reopened, or until the live wallpaper itself has been closed and reopened. Here's the code for the button that clears the preferences:
public boolean onPreferenceClick(Preference preference) {
AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
alertDialog.setMessage("Are you sure you want to reset all settings to default?");
alertDialog.setCancelable(true);
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences settings = getPreferenceManager().getSharedPreferences();
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
} });
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
} });
alertDialog.show();
return false;
}
});
How can I make these preferences apply immediately (without using apply() in place of commit(). Only 2 of my users are on API level 9 or higher). Thanks!
Perhaps your live wallpaper runs in a separate process? I don't believe that the listener will be fired if the change happens in a different process: you would probably need to use a BroadcastReceiver of some sort in the PreferenceActivity to notify the wall paper that things have changed. See http://groups.google.com/group/android-developers/browse_thread/thread/b90ab30a5d9e0803 for a very similar discussion that may provide more help.
精彩评论