Android Development Series (11): Read, add, delete, search for mobile phone address Book

Source: Internet
Author: User
First, Address Book introduction

The Address Book is an application of the Android phone, it is a contentprovider application, other applications can access the Address book, the contact person's crud operations.


Second, the Address book database structure introduction

First, we can find the Contacts2.db file in File Explorer view, which is the file in the Address Book.


Then, we open with SQLite, analyze its database structure: raw_contacts table:


data table:


mimetypes Table:


The structure of these three tables is finished, and then we'll talk about the connection between them:

Raw_contacts table holds contact's record ID: field name is _id

The data table holds the contact information: _id is the primary key, raw_contacts_id corresponds to the Raw_contacts table _id,mimetype_id field is mimetypes table _id

The Mimetypes table holds the properties of each record in the data table: _ID is the primary key and is the email type when 1


display, add, delete, find contact: 1, get all the contact person:

The public void Testcontacts () throws exception{uri uri = uri.parse ("content://com.android.contacts/contacts");
		Obtain a Contentresolver data-sharing object Contentresolver Reslover = GetContext (). Getcontentresolver (); Gets the cursor at the beginning of the contact by content://com.android.contacts/contacts this path to obtain Cursor Cursor = reslover.query (URI, NULL, NULL, NULL,
		
		NULL);
		All the code above can be replaced by this sentence: Cursor Cursor = cr.query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL); Uri.parse ("content://com.android.contacts/contacts") = = ContactsContract.Contacts.CONTENT_URI while ( Cursor.movetonext ()) {//Get contact ID String id = cursor.getstring (cursor.getcolumnindex (Android.provider.ContactsContrac  
			t.contacts._id)); Get Contact name String name = cursor.getstring (Cursor.getcolumnindex (android.provider.ContactsContract.Contacts.DISPLAY_
			NAME)); 
                    Get contact cell 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 ()) {//Get phone number (there may be multiple numbers) 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 results in the Logcat view log.i (TAG, sb.tostring ()); }
	}
2, find contact:

Obtain the contact's name based on the number public
	void Testcontactnamebynumber () throws exception{
		String = "a";
		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 Contact:

//add contact public void Testaddcontact () throws exception{uri uri = Uri.parse ("Content://com.android.con
		Tacts/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 email values.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, Bulk add contact person (because if by name, telephone, email way add words, one of the link error, contact person can also add up. But the bulk of the words is to add all of a sudden

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 ();
		
		Operations.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 "). Wit Hvalue("Data2", "2"). Build ();
		
		Operations.add (OP3);
	Resolver.applybatch ("com.android.contacts", operations); }
5. Delete Contact:

public void Testdelete () throws exception{  
	    String name = "Bruce Lee";  
	    ID  
	    uri uri = uri.parse ("Content://com.android.contacts/raw_contacts") based on name;  
	    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);  
	        Deletes the corresponding data  
	        resolver.delete (URI, "Display_name=?", New String[]{name}) in data according to the ID;  
	        URI = Uri.parse ("Content://com.android.contacts/data");  
	        Resolver.delete (URI, "Raw_contact_id=?", New String[]{id+ ""});  
	    }  
	  






Related Article

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.