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

Source: Internet
Author: User

Android Development Series (11): Reading, adding, deleting, and searching mobile phone 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: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + 7PGysdfaWQ8L3A + signature/Signature + PGJyPgo8L3A + CjxoMT7I/Signature + dPQtcTBqs + 1 ymujuw.vadi + Signature = "brush: java; "> 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
 
  
Operations = new ArrayList
  
   
(); 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 + ""});}}






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.