Part 1Article《Android contact analysis (1): Relationship between data, rawcontact, and contactThe relationship between contact tables has been explained clearly. This article will illustrate some examples to help you better understand them.
1. Read all the names and phone numbers of contacts:
/*** Query the names and phone numbers of all contacts */private void readcontacts () {stringbuilder sb = new stringbuilder (); contentresolver Cr = getcontentresolver (); // select * From contactscursor cursor = CR. query (contactscontract. contacts. content_uri, null, null); While (cursor. movetonext () {string id = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. _ id); string name = cursor. getstring (cursor. getcolum Nindex (contactscontract. contacts. display_name); int ihasphonenum = integer. parseint (cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. has_phone_number); sb. append (name + "("); If (ihasphonenum> 0) {cursor numcursor = CR. query (contactscontract. commondatakinds. phone. content_uri, null, contactscontract. commondatakinds. phone. contact_id + "=" + id, null, null); While (numcursor. movetonext () {String number = numcursor. getstring (numcursor. getcolumnindex (contactscontract. commondatakinds. phone. number); sb. append (number + ")");} numcursor. close ();} sb. append ("\ r \ n");} cursor. close (); If (! SB. tostring (). isempty () {log. D (TAG, "Contact: \ r \ n" + sb. tostring ());}}
1. First, search for all records from the contact;
2. read each contact_id and name;
3. query all phone numbers of the contact based on contact_id.
Ii. Name fuzzy search:
/*** Perform fuzzy search based on a word in the name * @ Param key */private void getfuzzyquerybyname (string key) {stringbuilder sb = new stringbuilder (); contentresolver Cr = getcontentresolver (); string [] projection = {contactscontract. phonelookup. display_name, contactscontract. commondatakinds. phone. number}; cursor = CR. query (contactscontract. commondatakinds. phone. content_uri, projection, contactscontract. contacts. display_n Ame + "like" + "'%" + key + "%'", null, null); While (cursor. movetonext () {string name = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. display_name); string number = cursor. getstring (cursor. getcolumnindex (contactscontract. commondatakinds. phone. number); sb. append (name + "("). append (number + ")"). append ("\ r \ n");} cursor. close (); If (! SB. tostring (). isempty () {log. D (TAG, "query contact: \ r \ n" + sb. tostring ());}}
3. Telephone number fuzzy search:
/*** Perform fuzzy search based on a word in the name * @ Param key */private void getfuzzyquerybyname (string key) {stringbuilder sb = new stringbuilder (); contentresolver Cr = getcontentresolver (); string [] projection = {contactscontract. phonelookup. display_name, contactscontract. commondatakinds. phone. number}; cursor = CR. query (contactscontract. commondatakinds. phone. content_uri, projection, contactscontract. contacts. display_n Ame + "like" + "'%" + key + "%'", null, null); While (cursor. movetonext () {string name = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. display_name); string number = cursor. getstring (cursor. getcolumnindex (contactscontract. commondatakinds. phone. number); sb. append (name + "("). append (number + ")"). append ("\ r \ n");} cursor. close (); If (! SB. tostring (). isempty () {log. D (TAG, "query contact: \ r \ n" + sb. tostring ());}}
4. Return pinyin using Chinese characters:
Private void getpinyinbyhanzi (string name) {contentvalues values = new contentvalues (); contentresolver Cr = getcontentresolver (); Uri rawcontacturi = CR. insert (contactscontract. rawcontacts. content_uri, values); long rawcontactid = contenturis. parseid (rawcontacturi); If (name. length ()> 0) {values. clear (); values. put (data. raw_contact_id, rawcontactid); values. put (data. mimetype, contactscontract. commondataki NDS. structuredname. content_item_type); values. put (contactscontract. commondatakinds. structuredname. given_name, name); CR. insert (contactscontract. data. content_uri, values); string [] projection = {"sort_key"}; string where = contactscontract. rawcontacts. contact_id + "=" + rawcontactid; cursor = CR. query (contactscontract. rawcontacts. content_uri, projection, where, null, null); If (cursor! = NULL) {cursor. movetofirst (); string pinyin = cursor. getstring (cursor. getcolumnindex ("sort_key"); log. D (TAG, pinyin); string res = ""; for (INT I = 0; I <pinyin. length (); I ++) {string temp = pinyin. substring (I, I + 1); If (temp. matches ("[A-Za-Z]") {res + = temp;} res = res. substring (0, Res. length ()-1); log. D (TAG, name + "translate = \" "+ Res. tolowercase (locale. getdefault () + "\" ") ;}} Cr. delete (contenturis. withappendedid (contactscontract. rawcontacts. content_uri, rawcontactid), null, null );}
This sectionCodeIn rawcontact, a "sort_key" field is used. The content of this field will be in pinyin + Chinese characters. For example, enter "Contact ", the content in "sort_key" is "Lian lianxi Department Ren ".
The idea is as follows:
1. Add a row in rawcontact and a rawcontact URI is returned. Raw contact ID can be obtained based on this URI;
2. Add a name to "data" based on raw contact ID;
3. query the rawcontact table by raw contact ID and retrieve the "sort_key" field;
4. Delete this record based on raw contact ID. The record will be automatically deleted in contact and data.