Created
October 11, 2012 18:19
-
-
Save JBirdVegas/3874450 to your computer and use it in GitHub Desktop.
ContentObserver quick example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Your link and code both dosen't work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the people who are really here to understand the use of Content Observer and how to implement it in our own Android App, you can visit
Use Android ContentObserver To React On Content Changes