Manage Android contacts and android contacts

Source: Internet
Author: User

Manage Android contacts and android contacts

Android provides the Contacts application to manage Contacts, and the Android system also provides ContentProvider for contact management, which allows other applications to manage contact data with ContentResolver.

For details about ContentProvider, ContentResolver, and Uri, refer to details!

Run the instance first:



Before performing operations on contacts, let's take a look at the main tables in the address book database:

1) RawContacts: stores the contact's ID. The _ id attribute is the primary key and declared as autoincrement. That is, you do not need to manually set other attributes;

2) mimetypes: the type of data to be stored, for example, "vnd. android. cursor. item/name "indicates" name "type data," vnd. android. cursor. item/phone_v2 "indicates telephone data;

Field name

Description

Phone number

Vnd. android. cursor. item/phone_v2

Name

Vnd. android. cursor. item/name

Email

Vnd. android. cursor. item/email_v2

Mailing address

Vnd. android. cursor. item/postal-address_v2

Organization

Vnd. android. cursor. item/organization

Photo

Vnd. android. cursor. item/photo

 

3) data stores specific data.

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 to the raw_contacts table. Each record represents a specific data. Our main data (such as email and phone) is stored in the data table;

5) StructuredName stores the contact name and its pinyin name.

When we insert a contact to the contact database, we usually insert a null value to the RawContacts table to obtain the returned rawContactId, that is, the contact ID, then, insert the contact name, phone number, and Email information to the Data table based on the ID.

Application instance:

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> * Contact managed by ContenPprovider </br> * This instance mainly allows you to add and query contacts. </br> * @ author JPH * Date: 2014.07.15 **/public class ContactProviderDemo extends Activity {ContentResolver resolver; Button btnSelect, btnAdd; EditText edtName, edtPhone, edtEmail; @ Overrideprotected void onCreate (Bundle savedInstanceState. 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 (); /*************************************** ********************************** * ***************/btnSelect. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // defines two arraylists used to store the contact information. final ArrayList <String> username = new ArrayList <String> (); final ArrayList <String> details = new ArrayList <String> (); // query the contact data Cursor cursor = resolver. query (ContactsContract. contacts. CONTENT_URI, null, null); // retrieve the contact name and IDwhile (cursor. moveToNext () {// obtain the contact name String name = cursor. getString (cursor. getColumnIndex (ContactsContract. contacts. DISPLAY_NAME); // obtain the contact IDString contactId = cursor. getString (cursor. getColumnIndex (ContactsContract. contacts. _ ID); username. add (name); // query the contact's phone number Cursor phones = resolver. query (ContactsContract. commonDataKinds. phone. CONTENT_URI, null, ContactsContract. commonDataKinds. phone. CONTACT_ID + "=" + contactId, null, null ); // define a detail list array to store the contact's phone number and EmailArrayList <String> detail = new ArrayList <String> (); // traverse the query result, obtain multiple phone numbers of the contact while (phones. moveToNext () {String phonenumber = phones. getString (phones. getColumnIndex (ContactsContract. commonDataKinds. phone. NUMBER); detail. add ("phone number:" + phonenumber);} phones. close (); // close the phones cursor to recycle resources // query the contact EmailCursor emails = resolver. query (ContactsContract. commonDataKinds. email. CONTENT_URI, null, ContactsContract. commonDataKinds. email. CONTACT_ID + "=" + contactId, null, null); // traverses the query result to obtain multiple phone numbers of the contact while (emails. moveToNext () {String emailAddress = emails. getString (emails. getColumnIndex (ContactsContract. commonDataKinds. email. DATA); detail. add ("Email:" + emailAddress);} emails. close (); // close the emails cursor to reclaim the resource details. add (detail);} cursor. close (); // load result. view view represented by the xml layout interface = getLayoutInflater (). inflate (R. layout. result, null); // obtain the ExpandableListView component ExpandableListView list = (ExpandableListView) view in the view. findViewById (R. id. list); // create ExpandableListAdapter to fill the data in list ExpandableListAdapter = new contacts () {// define a TextViewTextView geTextView () {TextView textView = new TextView (ContactProviderDemo. this); AbsListView. layoutParams lp = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, 64); textView. setLayoutParams (lp); textView. setPadding (36, 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 = geTextView (); textView. setText (getChild (groupPosition, childPosition ). toString (); return textView;} @ Overridepublic long getChildId (int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;} @ Overridepublic Object getChild (int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn details. get (groupPosition ). get (childPosition) ;}}; // set ExpandableListAdapterlist for ExpandableListView. setAdapter (adapter); // The create dialog box displays the query result new AlertDialog. builder (ContactProviderDemo. this ). setView (view ). setPositiveButton ("OK", null ). show ();}}); /*************************************** ********************************** * ***************/btnAdd. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // obtain 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 (); // to RawContacts. CONTENT_URI inserts a null value to obtain the returned rawContactIdUri = resolver. insert (RawContacts. CONTENT_URI, values); // parse rawContactIdlong rawContactId = ContentUris. parseId (uri); values. clear ();/*********** Add the contact name ************** // set the contact Idvalues. put (Data. RAW_CONTACT_ID, rawContactId); // sets the content type values. put (Data. MIMETYPE, StructuredName. CONTENT_ITEM_TYPE); // set the contact name (the name of the contact saved in StructuredName is the name of the contact, and its pinyin name) values. put (StructuredName. DISPLAY_NAME, name); // Add the Contact name resolver to the contact URi. insert (ContactsContract. data. CONTENT_URI, values); values. clear ();/*********** Add a contact's phone number *************/values. put (Data. RAW_CONTACT_ID, rawContactId); // sets the content type values. put (Data. MIMETYPE, Phone. CONTENT_ITEM_TYPE); // set the contact number values. put (Phone. NUMBER, phone); // set the contact phone type values. put (Phone. TYPE, Phone. TYPE_MOBILE); // Add the phone number resolver to the contact URi. insert (ContactsContract. data. CONTENT_URI, values); values. clear ();/*********** Add a contact Email ************** // set the contact Idvalues. put (Data. RAW_CONTACT_ID, rawContactId); // sets the content type values. put (Data. MIMETYPE, Email. CONTENT_ITEM_TYPE); // set the contact Emailvalues. put (Email. DATA, email); // Add the Contact Name resolver to the contact URi. insert (ContactsContract. data. CONTENT_URI, values); Toast. makeText (ContactProviderDemo. this, "Contact added successfully", Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. contact_provider_demo, menu); return true ;}}


 

 

 



Android controls the permission of third-party applications to access the address book

If you want to modify the permissions of the software you are developing, modify them in AndroidManifest. xml to add the corresponding permissions.

The access address book is:
<! -- Read contact permissions -->
<Uses-permissionandroid: name = "android. permission. READ_CONTACTS"/>
<! -- Call permission -->
<Uses-permissionandroid: name = "android. permission. CALL_PHONE"/>

What to do when developing the Android address book

First, you need to know the permissions required to obtain system contacts.
Secondly, you need to know the form in which your address book exists: the design of your address book
Finally, sort out the technologies used to design the address book and find out them one by one.
After completing these preparations, you can start to prepare the address book you want. Come on !!

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.