Skip to content

Instantly share code, notes, and snippets.

@chrishuan9
Forked from JBirdVegas/gist:3874450
Created January 24, 2014 19:12
Show Gist options
  • Save chrishuan9/8603999 to your computer and use it in GitHub Desktop.
Save chrishuan9/8603999 to your computer and use it in GitHub Desktop.
// references internal ContentObserver class
SettingsObserver observer = new SettingsObserver(new Handler());
// start watching for changes
observer.observe();
// where we do our work
updateSettings();
// Anonymous inner class to handle watching Uris
class SettingsObserver extends ContentObserver {
SettingsObserver(Handler handler) {
super(handler);
}
void observe() {
ContentResolver resolver = mContext.getContentResolver();
// set our watcher for the Uri we are concerned with
resolver.registerContentObserver(
Settings.System.getUriFor(Settings.System.MY_SUPER_SWEET_MOD_0),
false, // only notify that Uri of change *prob won't need true here often*
this); // this innerclass handles onChange
// another watcher
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.MY_SUPER_SWEET_MOD_1), false, this);
}
@Override
public void onChange(boolean selfChange) {
// if changes occurred in either of the watched Uris updateSettings()
// updateSettings() is a normal void method that will be called to handle all changes
// or you could just do your work here but presumable the same work will need to be done
// on load of your class as well... but you get the picture
updateSettings();
}
}
private void updateSettings() {
// do work
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment