Get the phone address book is the most commonly used Android small features, learned today, write down, mainly through the system comes from the content provider to provide data, we use the content recipient to get the corresponding data into the cursor, and then get the corresponding table field, the relevant field represents what meaning, I can only check it by myself.
Here is the code for the phone address list, for reference only:
Package Com.andy.phonecontact;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.content.contentresolver;import android.database.Cursor; Import Android.net.uri;import android.os.bundle;import Android.widget.listview;import android.widget.SimpleAdapter ;/** * @author zhang,tianyou * @version November 18, 2014 PM 10:00:46 */public Class Mainactivity extends Activity {private LISTV Iew select_contact; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); select_contact = (ListView) Findviewbyid (r.id.select_contact); list<map<string, string>> data = Getcontactinfo (); Select_contact.setadapter (New Simpleadapter ( Mainactivity.this, Data,r.layout.contact_item_view, new string[] {"Name", "Phone"},new int[] {r.id.tv_name, R.id.tv_ph One});} /** * Read the contact information in the phone * * @return */private list<map< String, string>> Getcontactinfo () {//Put all contacts in listlist<map<string, string>> list = new arraylist< Map<string, string>> ();//Get a content parser contentresolver resolver = Getcontentresolver ();//Get the content provider URL for the Contact table Raw_ Contacts table and data table uri URI = uri.parse ("content://com.android.contacts/raw_contacts"); Uri Datauri = Uri.parse ("Content://com.android.contacts/data"); cursor cursor = resolver.query (URI, new string[] {"contact_id"},null, NULL, NULL);//Get contact_id Get contact Idwhile (cursor.m Ovetonext ()) {String contact_id = cursor.getstring (0), if (contact_id! = null) {//specific one of the contacts map<string, string> map = New hashmap<string, string> ();//If the contact information for the data table is not a null query cursor Datacursor = Resolver.query (Datauri, new string[] {" Data1 "," MimeType "}," Contact_id=? ", new string[] {contact_id}, NULL), while (Datacursor.movetonext ()) {String data1 = da Tacursor.getstring (0); String mimetype = datacursor.getstring (1); System.out.println ("data1 = =" + data1 + "MimeType = =" + mimetype); if("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {SYSTEM.OUT.PRINTLN ("Tel:" + data1); Map.put ("Phone", data1);} else if ("Vnd.android.cursor.item/name". Equals (MimeType)) {System.out.println ("name:" + data1); Map.put ("name", Data1);}} List.add (map);//release cursor Datacursor.close ();}} Cursor.close (); return list;}}
2 Layout files in Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=" com.andy.phonecontact.MainActivity "> <linearlayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@+id/ Select_contact "/></relativelayout>
3 ListView Fill Layout Contact_item_view.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:id= "@+id/tv_name" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" name " android:textcolor=" #ff0000 " Android:textsize= "22SP"/> <textview android:id= "@+id/tv_phone" android:layout_width= "wrap _content " android:layout_height=" wrap_content " android:drawableleft=" @android:d rawable/ic_menu_call " android:text= "5558" android:textcolor= "#000000" android:textsize= "22SP"/></linearlayout >
4 Add the appropriate permissions:
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
Android Development to get the phone address Book