0

我在联系人上使用 ContentObserver。但是在这里我的问题是至少一旦我必须启动我的应用程序,否则我无法获得通知费用。我的代码是这样的

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();


       }

.... 高级谢谢。

4

2 回答 2

0

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.

于 2010-05-05T07:09:34.857 回答
0

如果您正在使用 Android 开发 UI 应用程序,那么无论您的代码或功能如何,只有在您启动应用程序后才能工作。开发服务或 Beckgroud 进程的更好选择,它将在设备的后台运行。你甚至不需要启动它。你有一个安卓屏幕键盘的活生生的例子。那是一种等待某种事件或某事并基于它弹出的应用程序。尝试开发服务,看看会发生什么......

于 2010-05-05T05:28:20.350 回答