Let's talk about the principle. As you know, android can monitor databases. However, I personally feel that this monitoring is not powerful. No matter how the monitoring is implemented, the final result returned is whether the database has changed or not. I did not tell us that the data in the database has been changed or deleted or added, most of the requirements cannot be solved. At the beginning, I encountered such a problem. No matter how early the information was, I did not find what I wanted. Most of the information on the Internet only tells you how to monitor the database, but I didn't tell you how to get specific data changes. Often success is forced by adversity. I searched for an English API and found the description of the version field in the contactscontract. rawcontacts class. The original Article is as follows: Version
Number that is updated whenever this row or its related data changes. this field can be used for Optimistic Locking of a raw contact. in Chinese, as long as the contact attributes are modified, they are automatically added. With this note, I tested the contacts on the android Virtual Machine and exported the sqlit database to check whether the version was changed. With this basis, the monitoring contact data can be well resolved.
My design idea is that when the user starts the program for the first time, the program traverses rawcontacts, combines all _ IDs and versions into strings, and then stores them with sharedpreferences. Then listen to the database. When the database changes, compare the data to know which data has been modified. If the sharedpreferences data does not exist, it indicates that it has increased, if the sharedpreferences data is greater than the data of the original database, it indicates an increase.First, I will put on a Java demo to demonstrate the ideas. Put the android code later. If you have any questions or have any mistakes, please advise. Public class contactjianting {public static void main (string ARGs []) {Init (); string STR = "1-2"; string a [] = Str. split ("-"); For (string: a) {system. out. println (string) ;}} Private Static void Init () {hashmap <string, string> A = new hashmap <string, string> (); hashmap <string, string> B = new hashmap <string, string> (); For (INT key = 1; key <= 1000; key ++) {. put (string. valueof (key), String. valueof (key) ); B. put (string. valueof (key), String. valueof (Key + 1);} set <string> aset =. keyset (); For (string: ASET) {system. out. println (string);} Long start = system. currenttimemillis (); For (INT I = 1; I <= 1000; I ++) {system. out. println ("Number" + I); If (! A. get (string. valueof (I )). equals (B. get (string. valueof (I) {system. out. println ("different data");} system. out. println ("-----------");} long end = system. currenttimemillis (); long sum = end-start; system. out. println ("Total time consumed" + sum) ;}} when the database is not connected, it takes about 40 ms to use hashmap, 1000 pieces of data. For users, there are few mobile phones with 1000 contacts.