Android uses read_contacts to read phone contacts

Source: Internet
Author: User

Instance code:
Package Com.example.readcontacts;import Java.io.inputstream;import Java.util.arraylist;import Android.app.listactivity;import Android.content.contentresolver;import Android.content.contenturis;import Android.content.context;import Android.content.intent;import Android.database.cursor;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.Bundle; Import Android.provider.contactscontract;import Android.provider.contactscontract.commondatakinds.phone;import Android.provider.contactscontract.commondatakinds.photo;import Android.text.textutils;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.listview;import Android.widget.textview;import Android.widget.AdapterView.OnItemClickListener; public class Mainactivity extends Listactivity {Context mcontext = null;/** Get library phon table field **/privaTe static final string[] phones_projection = new string[] {phone.display_name, phone.number, photo.photo_id, Phone.CONTAC t_id};/** Contact display name **/private static final int phones_display_name_index = 0;/** phone number **/private static final int phones_n Umber_index = 1;/** Avatar ID **/private static final int phones_photo_id_index = 2;/** Contact's ID **/private static final int Phon Es_contact_id_index = 3;/** Contact name **/private arraylist<string> mcontactsname = new arraylist<string> ();/** Contact head like **/private arraylist<string> mcontactsnumber = new arraylist<string> ();/** contact human head like **/private ArrayList <Bitmap> Mcontactsphonto = new arraylist<bitmap> (); ListView Mlistview = null; Mylistadapter myadapter = null; @Overridepublic void OnCreate (Bundle savedinstancestate) {Mcontext = This;mlistview = This . Getlistview ();/** get phone contacts contact information **/getphonecontacts (); myadapter = new Mylistadapter (this); Setlistadapter (Myadapter) ; Mlistview.setonitemclicklistener (New Onitemclicklistener () {@OverridepuBlic void Onitemclick (adapterview<?> adapterview, View view,int position, long ID) {//Call system method to call Intent Dialintent = New Intent (Intent.action_call, Uri.parse ("Tel:" + mcontactsnumber.get (position))); StartActivity (dialintent);}); Super.oncreate (savedinstancestate);} /** Get phone contacts contact information **/private void getphonecontacts () {Contentresolver resolver = mcontext.getcontentresolver ();// Get phone contact Cursor phonecursor = resolver.query (phone.content_uri,phones_projection, NULL, NULL, NULL); if (phonecursor! = NULL) {while (Phonecursor.movetonext ()) {//Get phone number string phonenumber = Phonecursor.getstring (Phones_number_index);// When the mobile number is empty or an empty field skips the current loop if (Textutils.isempty (phonenumber)) continue;//Gets the contact name string ContactName = Phonecursor.getstring (Phones_display_name_index);//Get contact Idlong ContactID = Phonecursor.getlong (phones_contact_id_ INDEX);//Get contact head like Idlong photoid = Phonecursor.getlong (Phones_photo_id_index);//Get contact with head like Bitampbitmap Contactphoto = null;//PhotoID greater than 0 indicates that the contact has an avatar assuming that the person does not have an avatar set to give him a default if (photoID > 0) {uri uri = Contenturis.withappendedid (ContactsContract.Contacts.CONTENT_URI, ContactID); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream (resolver, uri); Contactphoto = Bitmapfactory.decodestream ( input);} else {Contactphoto = Bitmapfactory.decoderesource (Getresources (), R.drawable.contact_photo);} Mcontactsname.add (ContactName); Mcontactsnumber.add (PhoneNumber); Mcontactsphonto.add (Contactphoto);} Phonecursor.close ();}} /** get phone SIM card contact everyone information **/private void getsimcontacts () {Contentresolver resolver = mcontext.getcontentresolver ();// Get Sims card Contact URI uri = uri.parse ("Content://icc/adn"); Cursor phonecursor = Resolver.query (URI, phones_projection, NULL, Null,null), if (phonecursor! = null) {while (phonecursor . MoveToNext ()) {//Get mobile number string phonenumber = Phonecursor.getstring (Phones_number_index);//When the mobile number is empty or the empty field skips the current loop if ( Textutils.isempty (PhoneNumber)) continue;//get contact name string contactName = Phonecursor.getstring (phones_display_name_ INDEX);//SIM card not connected to the head like McontactsnaMe.add (ContactName); Mcontactsnumber.add (PhoneNumber);} Phonecursor.close ();}} Class Mylistadapter extends Baseadapter {public Mylistadapter (context context) {Mcontext = context;} public int GetCount () {//Set drawing quantity return Mcontactsname.size ();} @Overridepublic Boolean areallitemsenabled () {return false;} Public Object getItem (int position) {return position;} public long getitemid (int position) {return position;} Public View GetView (int position, View Convertview, ViewGroup parent) {ImageView image = null; TextView title = null; TextView Text = null;if (Convertview = = NULL | | Position < mcontactsnumber.size ()) {Convertview = Layoutinflater.from (M Context). Inflate (r.layout.colorlist, null); image = (ImageView) Convertview.findviewbyid (r.id.color_image); title = ( TextView) Convertview.findviewbyid (r.id.color_title); text = (TextView) Convertview.findviewbyid (r.id.color_text);} Draw Contact Name Title.settext (mcontactsname.get (position));//Draw Contact Number Text.settext (Mcontactsnumber.get (position));// Draw Contact head like Image.setimAgebitmap (Mcontactsphonto.get (position)); return Convertview;}}} 
Note: User rights
<uses-permission android:name= "Android.permission.READ_CONTACTS"/><uses-permission android:name= " Ndroid.permission.CALL_PHONE "/>

Android uses read_contacts to read phone contacts

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.