Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

crud - How do I make my Android ContentObserver for ContactsContract detect a added, updated or deleted contact?

I am able to get a generic notification "that there was a change to the contacts DB", but I want to know the specific record that was inserted, updated, or deleted. Following is the code that gets registered and gets the onChange notification. Unfortunately, it is not specific which makes my processing exhaustive and inefficient.

Here is the code stub:

            if ((mNativeContactsObserver == null) && (mHandler == null)) {
            mHandler = new Handler(this.getMainLooper()) {
            };
            mNativeContactsObserver = new ContentObserver(mHandler) {
                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);

                    Bundle data = null;
                    Message message = mHandler.obtainMessage();
                    if (message != null) {
                        data = message.getData();
                        if (data != null) {
                            Logs.d(TAG, "Message = [" + message.toString() + "] data=[" + data.toString() + "]");
                            Logs.d(TAG, "Contents = [" + message.describeContents() + "]");
                        }
                    }

                    if (!selfChange) {
                        final Account accountListen = MySyncAdapter.lookupAccount(TAG, getApplicationContext(), getUserProfile().getAccountId(), AUTHORITY_MY_SYNC);
                        Logs.d(TAG, "onChange!? account: " + accountListen.name);
                        if (!ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY) 
                                && (!ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY))) {
                            Bundle extras = new Bundle();
                            extras.putInt(MySyncAdapter.EXTRA_SYNC_TYPE, MySyncAdapter.REQUEST_SYNC_NATIVE_CHANGED);
                            ContentResolver.requestSync(accountListen, ContactsContract.AUTHORITY, extras);
                        } else {
                            Logs.w(TAG, "There is a pending sync.  This request is ignored.");
                        }
                    }
                }
            };
        }
        Uri uriContactsListen = ContactsContract.Contacts.CONTENT_URI.buildUpon().appendEncodedPath("#").build();
        Logs.i(TAG, "Register listening for native contacts changes. [" + uriContactsListen + "]");
        cr.registerContentObserver(uriContactsListen, true, mNativeContactsObserver);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally at least i am able to detect is this Update/Delete/Insert look at my OnChange method as below

 @Override
       public void onChange(boolean selfChange) {
           super.onChange(selfChange);
           Log.e("ContactChangeObserver", "onChange");

           // 0 Update , 1 Delete , 2 Added
 // Get count from phone contacts
           final int currentCount = contactDBOperaion.getContactsCountFromPhone();

 // Get count from your sqlite database
           int mContactCount= DbContacts.getInstance().getContactsCount();

           if (currentCount < mContactCount) {
               // DELETE HAPPEN.
               Log.e("Status", "Deletion");
               contactDBOperaion.SyncContacts(1);
           } else if (currentCount == mContactCount) {
               // UPDATE HAPPEN.
               contactDBOperaion.SyncContacts(0);
           } else {
               // INSERT HAPPEN.
               Log.e("Status", "Insertion");
               contactDBOperaion.SyncContacts(2);
           }
       }

When Update occurred i update all contacts

When Insert occurred i am inserting newly added row , after checking isExist in existing database

When Delete occurred not found solution yet


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...