The system contact's source code package is located under the/data/data/com.android.providers.contacts folder and exported contacts2,db. To view the database structure with Sqllite:
Raw_contacts table: Save a reference to a contact, contact_id the ID of the person holding the contact
Data table: Details of contact information
raw_contact_id corresponding to the contact_id in the Raw_contacts table
Data1 represents the detailed data information of the contact person,
MIMETYPE_ID represents the detailed type, corresponding to the ID of the Mimetypes table
Mimetypes table: The detailed type of data.
So the contact information should be stored in the database. Need to use three sheets.
To get the contact information step:
1, Query raw_contacts table, get the ID of the contact person
2, according to the ID query data table, get contact details
3, according to the data type, to the Mimetypes table query data represents what
Because Google is implementing content providers. The data and Mimetypes tables are implemented with correlated queries, and the corresponding source code for processing the associated Contactsprovider2.java is:
Line No. 624: Sdataprojectionmap.put (Data.mimetype, Data.mimetype);
So the query operation has only two steps:
The processing code is:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.fragment_main);//querycontact (); Insertcontact (); private void Insertcontact () {///1, add an ID to the Raw_contact table, get the maximum ID value in this table to add 1Uri uri=uri.parse ("content:// Com.android.contacts/raw_contacts "); Uri datauri=uri.parse ("Content://com.android.contacts/data"); Cursor cursor=getcontentresolver (). Query (URI, new string[]{"contact_id"}, NULL, NULL, NULL); int id=1;if ( Cursor.movetolast ()) {id=cursor.getint (0);} int newid=id+1; Contentvalues values= New Contentvalues () values.put ("contact_id", newid); Getcontentresolver (). Insert (URI, values) ;//2. Insert data into the Contentvalues namevalue=new contentvalues () namevalue.put ("raw_contact_id", newid); Namevalue.put ( "MimeType", "Vnd.android.cursor.item/name"), Namevalue.put ("Data1", "Lili"), Getcontentresolver (). Insert (Datauri, Namevalue); Contentvalues emailvalue=new contentvalues (); Emailvalue.put ("raw_contact_id", NEWID); Emailvalue.put ("MimeType", "Vnd.android.cursor.item/email_v2"); Emailvalue.put ("Data1", "[email protected]); Getcontentresolver (). Insert (Datauri, emailvalue); Contentvalues phonevalue=new contentvalues ();p honevalue.put ("raw_contact_id", newid);p honevalue.put ("MimeType", " Vnd.android.cursor.item/phone_v2 ");p honevalue.put (" Data1 "," 18623567655 "); Getcontentresolver (). Insert (Datauri, Phonevalue);} private void Querycontact () {//Query raw_contacts table "is the database that operates the other application. Need to use the content provider, to the system source to find Contactsprovider manifest file, get the URI "//" Find Contactsprovider2.java know the System source database table name How to define "Uri uri=uri.parse (" Content://com.android.contacts/raw_contacts "),//data table corresponding Uriuri datauri=uri.parse (" content:// Com.android.contacts/data ");//This is just about getting contacts from the Raw_contacts table Idcursor cursor=getcontentresolver (). Query (URI, new string[]{"contact_id"}, NULL, NULL, NULL); while (Cursor.movetonext ()) {String id=cursor.getstring (0); Query the contents of the data table based on the contact ID obtained Cursor datacursor=getcontentresolver (). Query (Datauri, new string[]{"Data1", "MimeType"}, "Raw_contact_id=? ", New String[]{id}, NULL); while (Datacursor.movetonext ()) {System.out.println (datacursor.getstring (0) + ":" +datacursor.getstring (1)); } datacursor.close (); System.out.println ("======================="); } cursor.close ();}}
Additional: Contactsprovider The project manifest file content is able to view the URI and take advantage of the permissions that the content provider must set:
<provider android:name= "ContactsProvider2" android:authorities= "Contacts;com.android.contacts" an Droid:label= "@string/provider_label" android:multiprocess= "false" android:readpermission= "android.pe Rmission. Read_contacts "android:writepermission=" Android.permission.WRITE_CONTACTS "> <path-permission android:pathprefix= "/search_suggest_query" android:readpermission= "android.permission . Global_search "/> <path-permission android:pathprefix="/search_suggest_shortcut " android:readpermission= "Android.permission.GLOBAL_SEARCH"/> <path-permission Android:pathpattern= "/contacts/.*/photo" android:readpermission= "Android.permission.GLOBAL_SEARCH "/> <grant-uri-permission android:pathpattern=". * "/> </provider>
Definition of database table name in Contactsprovider2.java:
static {//Contacts URI matching table final urimatcher matcher = Surimatcher; Matcher.adduri (contactscontract.authority, "Contacts", contacts); Matcher.adduri (contactscontract.authority, "contacts/#", contacts_id); Matcher.adduri (contactscontract.authority, "Contacts/#/data", contacts_data); Matcher.adduri (contactscontract.authority, "contacts/#/suggestions", aggregation_suggestions); Matcher.adduri (contactscontract.authority, "contacts/#/suggestions/*", aggregation_suggestions); Matcher.adduri (contactscontract.authority, "Contacts/#/photo", Contacts_photo); Matcher.adduri (contactscontract.authority, "contacts/filter/*", Contacts_filter); Matcher.adduri (contactscontract.authority, "contacts/lookup/*", contacts_lookup); Matcher.adduri (contactscontract.authority, "contacts/lookup/*/#", contacts_lookup_id); Matcher.adduri (contactscontract.authority, "contacts/as_vcard/*", CONtacts_as_vcard); Matcher.adduri (contactscontract.authority, "contacts/as_multi_vcard/*", Contacts_as_multi_vcard); Matcher.adduri (contactscontract.authority, "contacts/strequent/", contacts_strequent); Matcher.adduri (contactscontract.authority, "contacts/strequent/filter/*", Contacts_strequent_filter); Matcher.adduri (contactscontract.authority, "contacts/group/*", Contacts_group); Matcher.adduri (contactscontract.authority, "raw_contacts", raw_contacts); Matcher.adduri (contactscontract.authority, "raw_contacts/#", raw_contacts_id); Matcher.adduri (contactscontract.authority, "Raw_contacts/#/data", raw_contacts_data); Matcher.adduri (contactscontract.authority, "raw_contacts/#/entity", raw_contact_entity_id); Matcher.adduri (contactscontract.authority, "raw_contact_entities", raw_contact_entities); Matcher.adduri (contactscontract.authority, "data", data); Matcher.adduri (ContactscontraCt. Authority, "data/#", data_id); Matcher.adduri (contactscontract.authority, "data/phones", phones); Matcher.adduri (contactscontract.authority, "data/phones/#", phones_id); Matcher.adduri (contactscontract.authority, "Data/phones/filter", Phones_filter); Matcher.adduri (contactscontract.authority, "data/phones/filter/*", Phones_filter); Matcher.adduri (contactscontract.authority, "data/emails", emails); Matcher.adduri (contactscontract.authority, "data/emails/#", emails_id); Matcher.adduri (contactscontract.authority, "data/emails/lookup/*", emails_lookup); Matcher.adduri (contactscontract.authority, "Data/emails/filter", Emails_filter); Matcher.adduri (contactscontract.authority, "data/emails/filter/*", Emails_filter); Matcher.adduri (contactscontract.authority, "Data/postals", postals); Matcher.adduri (contactscontract.authority, "data/postals/#", postals_id); Matcher.adduri (contactscontract.authority, "groups", GROUPS); Matcher.adduri (contactscontract.authority, "groups/#", groups_id); Matcher.adduri (contactscontract.authority, "Groups_summary", Groups_summary);}
Contact person using the Content Viewer OS (query, join)