Manage Android Communication Records

Source: Internet
Author: User

Android provides contacts applications to manage contacts, and the Android system also provides contentprovider for contact management, which allows other applications to contentresolver to manage contact data.

for ContentProvider, Contentresolver and URI details, you can refer to http://blog.csdn.net/fengyuzhengfan/article/details/ 37743963, it's not too much of a statement here!

Send the instance to run first:



Before you work with a contact, let's take a look at the main tables in the Contacts database:

1) Rawcontacts: The id,_id property of the contact person is the primary key, declared as AutoIncrement, that is, no manual setting is required, and other properties do not need to be manually set to have default values;

2) Mimetypes: The type of data stored, such as "Vnd.android.cursor.item/name" for the "name" type of data, "VND.ANDROID.CURSOR.ITEM/PHONE_V2" for "phone" type of data;

Field name

Description

Phone

Vnd.android.cursor.item/phone_v2

Name

Vnd.android.cursor.item/name

Mail

Vnd.android.cursor.item/email_v2

Correspondence Address

Vnd.android.cursor.item/postal-address_v2

Organization

Vnd.android.cursor.item/organization

Photo

Vnd.android.cursor.item/photo

3) data is stored in a specific database.

Field name

Description

data._id

"_ID"

Data.display_name

"Display_name"

Data.data1

"Data1"

Data.data2

"Data2"

data.raw_contact_id

"RAW_CONTACT_ID"

Data.mimetype

"MimeType"

4) The raw_contact_id attribute is used to connect the Raw_contacts table, each record represents a specific data, and our main data (email, phone, etc.) are stored in the datasheet;

5) The main structuredname is the name of the contact, its name and its phonetic name.

When we insert a contact into the correspondence database, we usually insert a blank line to the Rawcontacts table to get the rawcontactid that is the contact ID, and then insert the contact's name, phone number, and email information into the data table based on that ID.

Application Examples:

Package Com.jph.contactproviderdemo;import Java.util.arraylist;import Android.net.uri;import android.os.Bundle; Import Android.provider.contactscontract;import Android.provider.contactscontract.commondatakinds.email;import Android.provider.contactscontract.commondatakinds.phone;import Android.provider.contactscontract.commondatakinds.structuredname;import Android.provider.contactscontract.contacts.data;import Android.provider.contactscontract.rawcontacts;import Android.app.activity;import Android.app.alertdialog;import Android.content.contentresolver;import Android.content.contenturis;import Android.content.contentvalues;import Android.database.cursor;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.ViewGroup ; import Android.widget.abslistview;import Android.widget.baseexpandablelistadapter;import Android.widget.Button; Import Android.widget.edittext;import Android.widget.expandablelistadapter;import Android.widget.ExpandableLiStview;import android.widget.textview;import android.widget.toast;/** * describe:</br> * Contacts managed through Contenpprovider </br> * This example mainly implements the contact person's add and query </br> * @author JPH * date:2014.07.15 * */public class Contac Tproviderdemo extends Activity {contentresolver resolver; Button Btnselect,btnadd; EditText edtname,edtphone,edtemail; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); btnadd= (Button) Findviewbyid (R.id.add); btnselect= (Button) Findviewbyid (R.id.search); edtemail= (EditText) Findviewbyid (r.id.email); Edtname= (EditText) Findviewbyid (r.id.name ); edtphone= (EditText) Findviewbyid (r.id.phone);//Get Contentresolver object Resolver=getcontentresolver ();/************ Query Contact ***************************************************/ Btnselect.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//defines two ArrayList information to hold a contact final arraylist<string>Username=new arraylist<string> (); final arraylist<arraylist<string>>details=new ArrayList< Arraylist<string>> ();//query contact Data cursor cursor=resolver.query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL);//traversal cursor Remove contact name and Idwhile (Cursor.movetonext ()) {//Get contact name string name=cursor.getstring ( Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME));//Get in touch with idstring contactid=cursor.getstring ( Cursor.getcolumnindex (contactscontract.contacts._id)); Username.add (name);//Query Contact phone number cursor phones= Resolver.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, contactscontract.commondatakinds.phone.contact_id+ "=" +contactid, null, NULL);// Define an array of detail lists to hold the contact's phone number and emailarraylist<string>detail=new arraylist<string> ();//traverse the query results, Gets the contact's multiple phone number while (Phones.movetonext ()) {String phonenumber=phones.getstring (Phones.getcolumnindex ( ContactsContract.CommonDataKinds.Phone.NUMBER));d etail.add ("Phone number:" +phonenumber);} Phones.close ();//Close Phones Cursor Recycle resource//queryContact Emailcursor emails=resolver.query (ContactsContract.CommonDataKinds.Email.CONTENT_URI, NULL, contactscontract.commondatakinds.email.contact_id+ "=" +contactid, null, NULL);//Traverse query results to get multiple phone numbers for the contact while ( Emails.movetonext ()) {String emailaddress=emails.getstring (Emails.getcolumnindex ( ContactsContract.CommonDataKinds.Email.DATA));d Etail.add ("Email:" +emailaddress); Emails.close ();//Close emails cursor reclaim resource details.add (detail);} Cursor.close ();//load the Result.xml layout interface represents the view of Views View View=getlayoutinflater (). Inflate (R.layout.result, NULL);// Get the Expandablelistview component in view Expandablelistview list= (Expandablelistview) View.findviewbyid (R.id.list);// Create Expandablelistadapter to populate the list with data Expandablelistadapter adapter=new Baseexpandablelistadapter () {// Defines a Textviewtextview Getextview () {TextView textview=new TextView (contactproviderdemo.this) that displays contact information; Abslistview.layoutparams lp=new Abslistview.layoutparams (viewgroup.layoutparams.match_parent,64); TEXTVIEW.SETLAYOUTPARAMS (LP); textview.setpadding (0, 0, 0); Textview.settextsize (20);return TextView;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {//TODO auto-generated method Stubreturn true;} @Overridepublic Boolean hasstableids () {//TODO auto-generated method Stubreturn true;} @Overridepublic View getgroupview (int groupposition, Boolean Isexpanded,view Convertview, ViewGroup parent) {//TODO Auto -generated method Stubtextview Textview=getextview (); Textview.settext (Getgroup (groupposition). toString ()); return TextView;} @Overridepublic long getgroupid (int groupposition) {//TODO auto-generated method Stubreturn groupposition;} @Overridepublic int GetGroupCount () {//TODO auto-generated method Stubreturn username.size ();} @Overridepublic Object getgroup (int groupposition) {//TODO auto-generated method Stubreturn Username.get (groupposition );} @Overridepublic int getchildrencount (int groupposition) {//TODO auto-generated method Stubreturn Details.get ( groupposition). Size ();} @Overridepublic View getchildview (int groupposition, int childposiTion,boolean Islastchild, View Convertview, ViewGroup parent) {//TODO auto-generated method Stubtextview Textview=getext View (); Textview.settext (Getchild (Groupposition, childposition). toString ()); return TextView;} @Overridepublic long Getchildid (int groupposition, int childposition) {//TODO auto-generated method Stubreturn Childposi tion;} @Overridepublic Object getchild (int groupposition, int childposition) {//TODO auto-generated method Stubreturn DETAILS.G ET (groupposition). get (childposition);}};/ /Set Expandablelistadapterlist.setadapter (adapter) for Expandablelistview;//Create dialog box to display the query result new Alertdialog.builder ( contactproviderdemo.this). Setview (view). Setpositivebutton ("OK", null). Show ();}); Add Contact ***************************************************/ Btnadd.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Gets the contact information entered by the user string Name=edtname.gettext (). toString (); String Phone=edtphone.gettext (). ToSTring (); String Email=edtemail.gettext (). toString ();//define a contentvaluescontentvalues values=new contentvalues ();// Inserts a null value into the Rawcontacts.content_uri to get the returned Rawcontactiduri Uri=resolver.insert (Rawcontacts.content_uri, values);// Parse out Rawcontactidlong Rawcontactid=contenturis.parseid (URI); Values.clear ()/********** Add contact's name *************/// Set up Contact Idvalues.put (data.raw_contact_id, Rawcontactid);//Set Content type Values.put (Data.mimetype, Structuredname.content_ Item_type)///Set contact name (Structuredname is the name of the contact, its address name and its phonetic name) values.put (structuredname.display_name, name);// Add contact name to contact URI Resolver.insert (ContactsContract.Data.CONTENT_URI, values); Values.clear ();/********** Add Contact phone number * * * /values.put (data.raw_contact_id, Rawcontactid);//Set Content type Values.put (data.mimetype,phone.content_item_ type);//Set Contact phone values.put (phone.number, phone);//Set Contact phone type values.put (Phone.type, phone.type_mobile);// Add a phone number to a contact URI Resolver.insert (ContactsContract.Data.CONTENT_URI, values); Values.clear ();/********** Add Contact email*** Set up Contact Idvalues.Put (data.raw_contact_id, rawcontactid);//Set Content type Values.put (Data.mimetype, email.content_item_type);// Set up Contact Emailvalues.put (email.data,email);//Add contact name Resolver.insert to the contact URI (ContactsContract.Data.CONTENT_URI, values); Toast.maketext (Contactproviderdemo.this, "Add contact succeeded", Toast.length_short). Show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.contact_provider_demo, menu); return true;}}



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.