ContentObserver should call if and only if ContactsContract.Contacts.CONTENT_URI changes

前端 未结 2 678
既然无缘
既然无缘 2020-12-17 19:03

As my application uses content from android.provider.ContactsContract.Data (API > 11) and ContactsContract.Contacts.CONTENT_URI (API < 11) to po

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 19:28

    As android.provider.ContactsContract content provider has it's own complexity which makes ContentObserver tough to notify only on contacts content change except it's LAST_TIME_CONTACTED field as every fellow come across say these and so it is.

    One need to develop it's own logic whether contacts data get updated or not when ContentObserver notifies.

    Points to consider building logic whether contacts really get updated or not.

    • Checking on these basis of Last Time checked. Depends on the requirement.
    • Adding ContentObserver in Service which is STICKY so it can be there when contacts get change.

    Sync Phone Book Logic :- As I've maintained contacts using SQLite, so checking up whether that exist or not and w.r.t building logic.

    ContentValues values;
    Cursor cursor = Zname.getApplication().getContentResolver().query(DBConstant.All_Contacts_Columns.CONTENT_URI,null,DBConstant.All_Contacts_Columns.COLUMN_CONTACT_NUMBER+ "=?",new String[] { _contact.getContactNumber() },null);
    
     if (cursor.getCount() <= 0) {
            cursor.moveToFirst();
            Zname.getApplication().getContentResolver().delete(DBConstant.All_Contacts_Columns.CONTENT_URI,DBConstant.All_Contacts_Columns.COLUMN_CONTACT_NUMBER+ "?=",new String[] { _contact.getContactNumber() });
            Log.i(TAG, "Updating zname phonebook");
            values = new ContentValues();
            values.put(DBConstant.All_Contacts_Columns.COLUMN_CONTACT_ID,_contact.getContactId());
            values.put(DBConstant.All_Contacts_Columns.COLUMN_CONTACT_NUMBER,_contact.getContactNumber());
            values.put(DBConstant.All_Contacts_Columns.COLUMN_DISPLAY_NAME,_contact.getContactName());
            values.put(DBConstant.All_Contacts_Columns.COLUMN_ZNAME_DP_URL_SMALL,_contact.getContactPhotoUri().toString());
    
            Zname.getApplication().getContentResolver().insert(DBConstant.All_Contacts_Columns.CONTENT_URI,values);
    
            if (cursor != null)
                  cursor.close();
       }
    

提交回复
热议问题