質問

(working code extract added below)

My app needs to be notified of all inserts and deletes (and maybe updates, but less important) of contacts. This means when the app is started it will need a list of changes. While it is running it should be notified immediately (is it even possible to make changes to contacts outside the app while it is running?).

Should I be using a ContentObserver? Do I need a Service? Is there a way at app startup to get a list of changes that occurred since the last time the app ran?

Thanks.


ContentObserver does indeed work. However, for contacts, it does much less than I hoped for. You only get a notification that something has changed (in fact, you may get several notifications). You wont know what changed. Better than no notification though, I guess.

When you receive the notificaton, you'll have to run queries to find out if any of the contacts you are interested in have changed. If you need to check all of them, I think you'll be better off using a SyncAdapter.

Here's the code I ended up using. First a ContentObserver subclass; this receives notifications from whatever provider you register with (see next block of code):

  class MainContentObserver extends ContentObserver
  {
    public MainContentObserver (Handler handler)
    {
      super (handler);
    }

    @Override
    public void onChange (boolean selfChange)
    { 
      Message msg = handler.obtainMessage();
      msg.what = CONTACTS_CHANGED; // const int declared elsewhere
      msg.obj = null;
      handler.sendMessage (msg);
    }
  }

Here's the sceond block - this is the onCreate from your activity (or it could be in onResume). There are two important parts. One, I implement and instantiate a handler. This will receive "messages" from the observer, which runs in a separate thread, and relay them to my activity. The second piece is the creation of the observer, which happens through the register call.

  @Override
  public void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    // receive notices from our background threads.
    handler = new Handler()
    {
      @Override
      public void handleMessage (Message msg)
      {
        if (msg.what == CONTACTS_CHANGED)  // const int declared elsewhere
          System.out.println ("handler: contacts changed");
        else
          throw new IllegalArgumentException ("unrecognized handler message source: " + msg.what);
      }
    };

    // register content observer for contact changes
    contactsObserver = new MainContentObserver (handler);
    getContentResolver().registerContentObserver (ContactsContract.AUTHORITY_URI, true, 
                                                  contactsObserver);

    ... other initialization ...
  }

Finally, one more block of code - you need to unregister the observer or (I've read) you'll have a memory leak. (If you regsiter in onResume, be sure to unregister in onPause.)

  @Override
  public void onDestroy ()
  {
    super.onDestroy();
    getContentResolver().unregisterContentObserver (contactsObserver);
  }
役に立ちましたか?

解決

I know there is no broadcast for what you want to do. ContentObserver is what you have to go with. Also check: Native contact change notification

他のヒント

I think ContentObserver is better option, you can refer following ContentOberver

dealing with contacts.

i think you will have to look into the Broadcast Receiver for your question..

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top