Android Development to get the phone address Book

Source: Internet
Author: User

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;import;import Android.content.contentresolver;import android.database.Cursor; Import;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 (; 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[] {, 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://"); Uri Datauri = Uri.parse ("Content://"); 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("". Equals (MimeType)) {SYSTEM.OUT.PRINTLN ("Tel:" + data1); Map.put ("Phone", data1);} else if ("". 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= ""    xmlns:tools= "http// "    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= " 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

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: 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.