Call the address book and return the phone number in Android

Source: Internet
Author: User

Contactactivity. javapackage Yu. hong. contact; import android. app. activity; import android. content. contentresolver; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. provider. contactscontract; import android. view. view; import android. widget. edittext; public class contactactivity extends activity {// declare the text box private edittext et_mobile;   // Declare the name and phone numberPrivate string username, usernumber;   @ Override  Public void oncreate (bundle savedinstancestate ){  Super. oncreate (savedinstancestate );  Setcontentview (R. layout. Main );  // Obtain the text box by ID  Et_mobile = (edittext) This. findviewbyid (R. Id. mobiletel );  }      Public void gettelclick (view v ){      Startactivityforresult (new intent (   Intent. action_pick, contactscontract. Contacts. content_uri), 0 );   }@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); If (resultcode = activity. result_ OK ){ // Contentprovider displays data similar to a single database table // The contentresolver instance can find the specified contentprovider and obtain the data of contentprovider. Contentresolver recontentresolverol = getcontentresolver ();   // URI. Each contentprovider defines a unique public URI to specify its dataset.  Uri contactdata = data. getdata ();  // Query is the input of Uri and other parameters, where URI is required and others are optional. If the system can find the contentprovider corresponding to the URI, a cursor object is returned.  Cursor cursor = managedquery (contactdata, null );   Cursor. movetofirst ();   // Obtain the name in the data table  Username = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));   // The condition is the contact ID.  String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));   // Obtain the phone number in the data table. The condition is the contact ID, because there may be multiple phone numbers  Cursor phone = recontentresolverol. Query (contactscontract. commondatakinds. Phone. content_uri,     Null,     Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid,     Null,     Null );   While (phone. movetonext ()){   Usernumber = phone. getstring (phone. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));   Et_mobile.settext (usernumber + "(" + username + ")");   }       } }   } Layout main. xml <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent"  Android: layout_height = "fill_parent"  Android: Orientation = "vertical">  <Edittext     Android: layout_width = "fill_parent"   Android: layout_height = "wrap_content"   Android: hint = "@ string/mobiletel"   Android: Id = "@ + ID/mobiletel"   />   <Button     Android: layout_width = "fill_parent"   Android: layout_height = "wrap_content"   Android: text = "@ string/btn_gettel"   Android: onclick = "gettelclick"       /> Androidmanifest. xml <! -- Add the permission to read and write contact information -->  <Uses-Permission Android: Name = "android. Permission. read_contacts"/>  <Uses-Permission Android: Name = "android. Permission. write_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.