Android to see if the Address book contacts has changed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.