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"/>