I have used this solution to add a badge counter to my app icon. I am using the counter to show how many items there are in the app's queue_table
that are waiting to be sent to the server.
First of all, I have create a MyBootReceiver
class that updates the badge count when the device boots. This part works fine.
The part I need advice on is the right way to keep the badge count updated whenever the queue is updated. (The queue can be updated by various components of the app - e.g., from the user manually adds items to the queue and from MyIntentService
sending queued items to the server).
My queue_table
is accessible via a ContentProvider
in the app, so what I essentially need to know is the best way to monitor this content provider for changes (so the badge icon can be updated accordingly).
I am wondering if the best (or only) solution would be for me to create a MyApplication
class that registers a ContentObserver
in its onCreate
method - e.g.,
MyApplication.java
@Override
public void onCreate() {
super.onCreate();
/*
* Register for changes in queue_table, so the app's badge number can be updated in MyObserver#onChange()
*/
Context context = getApplicationContext();
ContentResolver cr = context.getContentResolver();
boolean notifyForDescendents = true;
myObserver = new MyObserver(new Handler(), context);
cr.registerContentObserver(myContentProviderUri, notifyForDescendents, myObserver);
}
Also, if I do use such a solution, would I need to worry about unregistering myObserver
and, if so, how would I do that in MyApplication
?