Android notify when phone book is updated(Content Observer)

后端 未结 3 1302
灰色年华
灰色年华 2021-01-02 04:28

I want to get a notification on my phone if there is any change in the contact database(add,delete).Right now i am using ContentObserver to get notified.Following is my code

相关标签:
3条回答
  • 2021-01-02 05:03

    Observer does not provide the information that which contact is added/update/deleted. To get to know this save the contacts in your own DB table and when observer send the change notification check it with system's Contacts.

    0 讨论(0)
  • 2021-01-02 05:05

    I have changed onChange code to this.

    @Override
    public void onChange (boolean selfChange)
    {
        this.onChange(selfChange, null);
    }
    
    @Override
    public void onChange (boolean selfChange,Uri uri)
    {
      Cursor cursor = mCntxt.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null,ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP + " Desc");
        if (cursor.moveToNext()) {
            String id = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.w("Contact ID", id);
            Log.w("Person Name",name);
           }
    }
    

    Hope this helps..

    0 讨论(0)
  • 2021-01-02 05:10

    I think it may be possible using a Broadcast manager / receiver http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

    0 讨论(0)
提交回复
热议问题