How to listen contact inserted/updated/deleted in address book

风格不统一 提交于 2019-11-28 21:23:19

The thing about onChange is that it gets called both for delete/add/update so you can't just count the number of contacts since one might have been deleted and one added then you have a changed contact book, but same count. However looking at the version column you should be able to assess which contact is updated or not (after you've obtained one complete copy of the contact book already). Simply check if the version is greater than the one you have already (for the current contact).

In accordance with Magnus' answer, use this column to retrieve the version code

ContactsContract.RawContacts.VERSION

i did in this way and it worked for me for call Log and ContactNumber Change.

in onChange

execute a Query with contentResolver(), and put the data in a List
then something is deleted again execute
 a Query with contentResolver(), and assign it to tempList.
now compare the List and tempList



@Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);    
        final int List = totalContacts();

if (tempList < List) {
something is deleted
then remove the tempList from List you will get the deleted number

}else if (tempList == List) {
something is updated
then remove the tempList from List you will get the deleted number
}else {
       something is added(reverse the Lists)
then remove the List from tempList you will get the deleted number
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!