问题
I need to have my application listen for changes in addressbook in Android. I have read that it can be done using ContentObserver
and listening for changes in ContactsContract.Contacts
. It seems the lifecycle of the ContentObserver ends when the application is closed.
How do I make the ContentObserver work even if the application wasn't opened?
回答1:
How do I make the ContentObserver work even if the application wasn't opened?
That is not possible. The point behind a ContentObserver
is to be notified of changes that might affect a running app, such as changes to data that need to be reflected in an activity showing that data. If your app is not running, you cannot have a ContentObserver
.
回答2:
register your Content Observer in the OnStartCommand with return START_STICKY;
instead of registering it in the onCreate. It works for me.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getContentResolver().registerContentObserver(uri,true, new SMSObserver(new Handler(), getBaseContext()));
return START_STICKY;
}
来源:https://stackoverflow.com/questions/19249566/how-to-make-contentobserver-work-without-having-application-opened