问题
I am using ContentObserver on contacts. But here my problem is atleast once i have to launch my application other wise i am unable to get notification chages. My code like this
ContactsContentObserver cco = new ContactsContentObserver(handler);
ContentResolver contentResolver = getContentResolver();
contentResolver.registerContentObserver(RawContacts.CONTENT_URI,
true, cco);
}
private class ContactsContentObserver extends ContentObserver
{
public ContactsContentObserver(Handler h)
{
super(h);
}
public void onChange(boolean selfChange)
{
System.out.println("##########SOMEBODAY CHANGED ANYTHING AT THE CONTACTS");
Toast.makeText(getApplication(),"####Updated####",Toast.LENGTH_LONG).show();
}
.... Adv thanks.
回答1:
IF you are developing an UI application with Android then whatever your code or functionality will only works once you launch your application. The better option for you to develop the service or Beckgroud process, which will be running in background on the device. And you dont even need to launch it. You are having an live example of OnScreen Keyboard of Android. That is one kind of application which wait for some sort of event or something and based on that it pop's up. Try to develop service and see what happens....
回答2:
I assume you are wanting the ContentObserver
to automatically run when something changes in the contacts. The problem is, something will need to call the code that registers that listener. What you probably could do is create a service that starts when the device finishes booting, registers the ContentObserver
and then exits. The service does not need to continuously run as the ContentProvider
framework will automatically call your code. Look into registering to receive the BOOT_COMPLETED_ACTION
intent as per this post.
来源:https://stackoverflow.com/questions/2770757/reg-contact-changes-in-android