How to make ContentObserver work without having application opened?

こ雲淡風輕ζ 提交于 2019-12-25 02:09:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!