Android Get mobile Contact information _android

Source: Internet
Author: User

Android how to get mobile phone contact information, this article for everyone to announce.

Get Mobile Contact Information step:

1. Get Contentresolver

Contentresolver resolver = Getcontentresolver ();

2, resolver.query (*) query information

Inquire about the URI:ContactsContract.RawContacts.CONTENT_URI of mobile phone contact person
Search for mobile phone contact URI:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
Query the URI:ContactsContract.CommonDataKinds.Email.CONTENT_URI of a contact's mailbox

3, according to the contact name _id inquires the corresponding mobile phone number and the mailbox

Find raw_contact_id and contact _id entries in cell phone number and mailbox query

The main code is as follows:

Mainactivity.java

Package com.noonecode.contentresolvercontacts;
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.os.Bundle;
Import Android.provider.ContactsContract.CommonDataKinds.Email;
Import Android.provider.ContactsContract.CommonDataKinds.Phone;
Import android.provider.ContactsContract.RawContacts;
Import Android.widget.ListView;

Import Android.widget.SimpleAdapter;
 public class Mainactivity extends activity {private ListView mlvshow;
 Private list<map<string, string>> dataList;

 Private Simpleadapter adapter;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Mlvshow = (ListView) Findviewbyid (r.id.lv_show);
 DataList = Getdatalist (); adapter = new Simpleadapter (this, dataList, r.layout.simple_contacts_item//, new StriNg[] {"Name", "number", "email"}//, new int[] {r.id.tv_name, r.id.tv_number, r.id.tv_email});
 Mlvshow.setadapter (adapter); /** * Load Data * * @return/private list<map<string, string>> getdatalist () {list<map<string,
 string>> list = new arraylist<map<string, string>> ();
 Contentresolver resolver = Getcontentresolver (); Cursor Cursor = Resolver.query (Rawcontacts.content_uri, new string[] {rawcontacts._id, rawcontacts.display_name_
 PRIMARY}//, NULL, NULL, NULL);
  while (Cursor.movetonext ()) {map<string, string> Map = new hashmap<string, string> (); String id = cursor.getstring (cursor.getcolumnindex (rawcontacts._id));/"_id" string name = Cursor.getstring (

  Cursor.getcolumnindex (rawcontacts.display_name_primary));//"Display_name" Map.put ("name", name); Contact number Cursor Phonecursor = Resolver.query (phone.content_uri//, new string[] {phone.number}//"data1", "raw_ Contact_id=? ", new string[] {ID}, NULL);
  if (Phonecursor.movetonext ()) {String number = phonecursor.getstring (Phonecursor.getcolumnindex (Phone.number));
  Map.put ("number", number); }//Contact mailbox Cursor emailcursor = Resolver.query (email.content_uri//, new string[] {email.address}//"data1",
  "Raw_contact_id=", new string[] {ID}, NULL);
  if (Emailcursor.movetonext ()) {String email = emailcursor.getstring (Emailcursor.getcolumnindex (email.address));
  Map.put ("email", email);
 } list.add (map);
 } return list;
 }
}

Master Layout 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 "
 tools:context= "com.noonecode.contentresolvercontacts.MainActivity" >

 <listview
 android:id= "@ +id/lv_show "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "/>

</ Relativelayout>

Simple_contacts_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android: padding= "10DP" >

 <textview
 android:id= "@+id/tv_name"
 android:layout_width= "Wrap_content"
 android:layout_height= "Wrap_content"
 android:text= "name"
 android:textsize= "22sp"/>

 < TextView
 android:id= "@+id/tv_number"
 android:layout_width= "wrap_content"
 android:layout_height= "Wrap_content"
 android:layout_below= "@id/tv_name"
 android:text= "number"/>

 <textview
 Android:id= "@+id/tv_email"
 android:layout_width= "wrap_content"
 android:layout_height= "Wrap_content"
 android:layout_below= "@id/tv_name"
 android:layout_marginleft= "10DP"
 android:layout_torightof= " @id/tv_number "
 android:text=" email "/>

</RelativeLayout>

To read a contact's permissions:

<uses-permission android:name= "Android.permission.READ_CONTACTS"/>

Final Effect Diagram:

Attention:

Check to see if the permission to read the contact is given to the application;
This example reads the contact information in the main thread, which is inefficient, and if it is efficient, find other ways to do it.

SOURCE Download Address: Http://xiazai.jb51.net/201610/yuanma/AndroidContactsDemo (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.