Android Development Series (11): Reading, adding, deleting, and searching mobile phone Address Book, android address book

Source: Internet
Author: User

Android Development Series (11): Reading, adding, deleting, and searching mobile phone Address Book, android address book
1. Address Book Introduction

Address book is an app that comes with the Android mobile phone. It is a ContentProvider app. Other apps can access the address book and perform CRUD operations on contacts.


II. Introduction to the database structure of the address book

First, we can find the contacts2.db File in the File Explorer view, which is the address book File.


Then, we use SQLite to open and analyze its database structure:

Raw_contacts table:


Data Table:


Mimetypes table:


The structure of these three tables has been introduced. Let's talk about the relationship between them:

Raw_contacts table stores the contact's record ID: field name is _ id

Data Table stores the contact information: _ id is the primary key, raw_contacts_id corresponds to the _ id of the raw_contacts table, and mimetype_id corresponds to the _ id of the mimetypes table

In the mimetypes table, the attribute of each record in the data table is: _ id is the primary key, and 1 is the email type.


3. display, add, delete, and search for contacts: 1. Obtain all contacts:

Public void testContacts () throws Exception {Uri uri = Uri. parse ("content: // com. android. contacts/contacts "); // obtain the ContentResolver data sharing object ContentResolver reslover = getContext (). getContentResolver (); // get the cursor starting from the contact by using content: // com. android. the contacts/contacts path obtains Cursor cursor = reslover. query (uri, null, null); // all the above Code can be replaced by this sentence: Cursor cursor = cr. query (ContactsContract. contacts. CONTENT_URI, null, null); // Uri. parse ("content: // com. android. contacts/contacts ") = ContactsContract. contacts. CONTENT_URIwhile (cursor. moveToNext () {// obtain the contact IDString id = cursor. getString (cursor. getColumnIndex (android. provider. contactsContract. contacts. _ ID); // obtain the contact name String name = cursor. getString (cursor. getColumnIndex (android. provider. contactsContract. contacts. DISPLAY_NAME); // obtain the contact's mobile phone 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 () {// obtain the 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 so that you can view the result Log in the LogCat view. I (TAG, sb. toString ());}}
2. Search for contacts:

// Obtain the public void testContactNameByNumber () throws Exception {String number = "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); if (cursor. moveToFirst () {String name = cursor. getString (0); Log. I (TAG, name);} cursor. close ();}

3. Add a contact:

// Add the 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 the 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 the phone number 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", "1234120155@qq.com"); resolver. insert (uri, values );}
4. Add contacts in batches (if you add Contacts by name, phone number, or Email, an error occurs in one of the links, and the contacts can also be added. However, if you batch add all of them at once)

// 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 (); operations. add (op1); uri = Uri. parse ("content: // com. android. contacts/data "); // Add the 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 the 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 a contact:

Public void testDelete () throws Exception {String name = ""; // calculate id Uri uri = Uri Based on name. 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 in data based on the id. delete (uri, "display_name =? ", New String [] {name}); uri = Uri. parse ("content: // com. android. contacts/data "); resolver. delete (uri, "raw_contact_id =? ", New String [] {id + ""});}}







What books can be used to add, delete, edit, and browse an Android phone address book?

Depending on how much you want to learn, the crazy Android handout is quite good. The lecture is very detailed and comprehensive.

Android phones accidentally delete contacts in the address book. How can this problem be solved? Very worried

If you are not synchronized to Gmail

Backup, pea pod, 360 security guard, 360 mobile assistant, Sim Card Backup

.... Then I can only mourn for you!

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.