Purpose: Determine if the Address book has changed
According to: See the version constant in the Contactscontract.rawcontacts class, which is read-only and changes the value when the Address book changes
Method: The version value is corresponding to each address book data, if there are 100, then there are 100 this value, I said that the use of the inference method is this
1. Get all version values, make up a string
2, because the string may be very long, so the use of MD5 transform short string
3, compare with the previous string, save the new to Sharedpreferences
The following three pieces of code are implemented to see if the Address book changes
/** * Get Address Book version * * @return * * private string Getcontactsversion () {string version = NULL; StringBuffer sb = new StringBuffer (); Cursor Raws=null; try{raws = Mcontext.getcontentresolver (). query (ContactsContract.RawContacts.CONTENT_URI, NULL, NULL, NULL, NULL); while (Raws.movetonext ()) {Version = Raws.getstring (Raws.getcolumnindex (ContactsContract.RawContacts.VERSION )); Sb.append (version); }}catch (Exception e) {e.printstacktrace (); }finally {if (raws!=null) {raws.close (); }} return sb.tostring (); }
/** * String version Convert to MD5 format * * @param s * @return */private string StringToMd5 (string s) {byte[] value = S.get Bytes (); try {messagedigest MD = messagedigest.getinstance ("MD5"); Md.update (value); byte[] temp = md.digest (); StringBuilder sb = new StringBuilder (); for (byte b:temp) {Sb.append (integer.tohexstring (b & 0xff)); } String md5version = Sb.tostring (); Editor editor = Spf.edit (); Editor.putstring ("Contact_version", md5version); Editor.commit (); return md5version; } catch (NoSuchAlgorithmException e) {e.printstacktrace (); } return null; }
/** * Infer that there is not an update to the Address book return True indicates that there is an update, return false indicates no update * /Public Boolean iscontactupdate () { String oldversion = Spf.getstring ("Contact_version", "first"); String newversion = StringToMd5 (Getcontactsversion ()); if (log.isloggable ("version", Log.debug)) { log.d ("version", "old version---" + oldversion); LOG.D ("Version", "New version---" + newversion); } Return (!newversion.equals (oldversion)); }
Android to see if the Address book contacts has changed