First, Address Book introduction
The Address Book is an app that comes with Android phones, which is an ContentProvider app that allows other apps to access contacts and make CRUD operations on contacts.
Ii. Introduction to the structure of contacts database
First, we can find the contacts2.db file in the File Explorer view, which is the address book
Then we opened it with SQLite and analyzed its database structure:
Raw_contacts table:
Data table:
Mimetypes table:
The structure of the three tables is finished, and then we say the links between them:
Raw_contacts The record ID of the Contact: field name is _id
The data table holds the contact information: _id is the primary key, and the _id,mimetype_id field corresponding to the Raw_contacts table corresponds to the Mimetypes table _id
The Mimetypes table holds the properties of each record of the data table: _ID is the primary key, and 1 is the email type
Third, display, add, delete, find contacts: 1, get all the contacts:
public void Testcontacts () throws Exception{uri Uri = Uri.parse ("content://com.android.contacts/contacts");// Get a Contentresolver data-sharing object Contentresolver Reslover = GetContext (). Getcontentresolver ();//Gets the cursor that starts in the contact by content:// Com.android.contacts/contacts This path gets the cursor cursor = reslover.query (URI, NULL, NULL, NULL, or null);// All the code above can be replaced by this phrase: cursor cursor = cr.query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL);//uri.parse (" Content://com.android.contacts/contacts ") = = ContactsContract.Contacts.CONTENT_URIwhile (Cursor.movetonext ()) {// Get contact Idstring id = cursor.getstring (cursor.getcolumnindex (android.provider.contactscontract.contacts._id)); Get Contact Name String name = Cursor.getstring (Cursor.getcolumnindex (android.provider.ContactsContract.Contacts.DISPLAY_ NAME));//Get contact mobile number Cursor phone = reslover.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ID, NULL, NULL); StringBuilder sb = new StringBuilder ("contactid="). Append (ID). Append (name), while (Phone.movetonext ()) {//Get phone number (multiple numbers may exist) int Phonefieldcolumnindex = Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER); String PhoneNumber = phone.getstring (Phonefieldcolumnindex); Sb.append (phonenumber+ "www");} Create a log that allows you to view the results log.i (TAG, sb.tostring ()) in the Logcat view;}}
2. Find Contacts:
Gets the name of the contact according to the number public void Testcontactnamebynumber () throws exception{string # = "110"; Uri uri = uri.parse ("content://com.android.contacts/data/phones/filter/" +number); Contentresolver resolver = GetContext (). Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{android.provider.contactscontract.data.display_name}, NULL, NULL, NULL); if (Cursor.movetofirst ()) {String name = cursor.getstring (0); LOG.I (TAG, name);} Cursor.close ();}
3. Add a Contact:
Add contact public void Testaddcontact () throws Exception{uri Uri = Uri.parse ("content://com.android.contacts/raw_contacts "); Contentresolver resolver = GetContext (). Getcontentresolver (); Contentvalues values = new Contentvalues (); Long ContactID = Contenturis.parseid (Resolver.insert (URI, values); URI = Uri.parse ("Content://com.android.contacts/data");//Add Name Values.put ("raw_contact_id", ContactID); Values.put ( Data.mimetype, "Vnd.android.cursor.item/name"); Values.put ("Data1", "xiaoming"); Resolver.insert (URI, values); Values.clear ();//Add Phone Values.put ("raw_contact_id", ContactID); Values.put (Data.mimetype, "vnd.android.cursor.item/ Phone_v2 "); Values.put (" Data1 "," 1234120155 "); Resolver.insert (URI, values); Values.clear ();//Add Emailvalues.put (" raw_contact_id ", ContactID); Values.put (Data.mimetype," vnd.android.cursor.item/email_v2 "); Values.put (" Data1 "," [ Email protected] "); Resolver.insert (URI, values);}
4. Add Contacts in bulk (because if you add them by name, phone, email), one of the links goes wrong and the contact can be added. But the bulk of the words is to add all at once)
Bulk add public void TestAddContact2 () throws Exception{uri Uri = Uri.parse ("content://com.android.contacts/raw_contacts "); Contentresolver resolver = GetContext (). Getcontentresolver (); arraylist<contentprovideroperation> operations = new arraylist<contentprovideroperation> (); Contentprovideroperation OP1 = Contentprovideroperation.newinsert (URI). Withvalue ("account_name", null). Build (); O Perations.add (OP1); uri = Uri.parse ("Content://com.android.contacts/data");//Add name contentprovideroperation OP2 = Contentprovideroperation.newinsert (URI). Withvaluebackreference ("raw_contact_id", 0). Withvalue ("MimeType", " Vnd.android.cursor.item/name "). Withvalue (" Data2 "," Bruce Lee "). Build (); Operations.add (OP2);// Add phone number Contentprovideroperation op3 = Contentprovideroperation.newinsert (URI). Withvaluebackreference ("raw_contact_ ID ", 0). Withvalue (" MimeType "," Vnd.android.cursor.item/phone_v2 "). Withvalue (" Data1 "," 1234120155 "). Withvalue (" Data2 "," 2 "). Build (); Operations.add (OP3); Resolver.applybatch (" com.android.coNtacts ", operations);}
5. Delete Contact:
public void Testdelete () throws exception{ String name = "Bruce Lee"; By name, id uri uri = uri.parse ("content://com.android.contacts/raw_contacts"); Contentresolver resolver = This.getcontext (). Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{data._id}, "Display_name=?", New String[]{name}, NULL); if (Cursor.movetofirst ()) { int id = cursor.getint (0); Delete the corresponding data resolver.delete (URI, "Display_name=?", new String[]{name}) according to the ID; URI = Uri.parse ("Content://com.android.contacts/data"); Resolver.delete (URI, "Raw_contact_id=?", New String[]{id+ ""}); } }
Android Development Series (11): Read, add, delete, find the phone address Book