Import Android. App. activity;
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. Button;
Import Android. widget. edittext;
Import Android. widget. textview;
Import Android. widget. Toast;
Public class phonebook extends activity {
/* Declare four UI variables and a constant as the return value received by the activity */
Private textview mtextview01;
Private button mbutton01;
Private edittext medittext01;
Private edittext medittext02;
Private Static final int pick_contact_subactivity = 2;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
/* Use findviewbyid to construct a textview, two edittexts, and one button object **/
Mtextview01 = (textview) findviewbyid (R. Id. mytextview1 );
Medittext01 = (edittext) findviewbyid (R. Id. myedittext01 );
Medittext02 = (edittext) findviewbyid (R. Id. myedittext02 );
Mbutton01 = (button) findviewbyid (R. Id. mybutton1 );
/* Set onclicklistener to allow users to search for contacts when clicking the button */
Mbutton01.setonclicklistener (New button. onclicklistener ()
{
// @ Override
Public void onclick (view V)
{
// Todo auto-generated method stub
/* Construct a URI to obtain the contact's resource location */
// URI uri = URI. parse ("content: // contacts/people /");
/* Use intent to retrieve contact data and return the selected value */
// Intent = new intent (intent. action_pick, Uri );
/* Start a new activity and expect it to return a value */
// Startactivityforresult (intent, pick_contact_subactivity );
Startactivityforresult
(
New intent (intent. action_pick,
Android. provider. contactscontract. Contacts. content_uri ),
Pick_contact_subactivity );
}
});
}
@ Override
Protected void onactivityresult (INT requestcode, int resultcode,
Intent data)
{
// Todo auto-generated method stub
Try
{
Switch (requestcode)
{
Case pick_contact_subactivity:
Final URI uriret = data. getdata ();
If (uriret! = NULL)
{
Try
{
/* Android. Permission. read_contacts permissions must be granted */
Cursor c = managedquery (uriret, null,
Null );
/* Move cursor to the data frontend */
C. movetofirst ();
/* Obtain the contact name */
String strname =
C. getstring (C. getcolumnindexorthrow (contactscontract. Contacts. display_name ));
/* Write the name to edittext01 */
Medittext01.settext (strname );
/* Obtain the contact's phone number */
Int contactid =
C. getint (C. getcolumnindex (contactscontract. Contacts. _ id ));
Cursor phones = getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri, null,
Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid,
Null, null );
Stringbuffer sb = new stringbuffer ();
Int typephone, restype;
String numphone;
If (phones. getcount ()> 0)
{
Phones. movetofirst ();
/* 2.0 allow the user to set multiple phone numbers, but in this example, only one group of phone numbers are available for demonstration */
Typephone = phones. getint (
Phones. getcolumnindex (contactscontract. commondatakinds. Phone. type ));
Numphone = phones. getstring (
Phones. getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
Restype =
Contactscontract. commondatakinds. Phone. gettypelabelresource (typephone );
SB. append (getstring (restype) + ":" + numphone
+ "/N ");
/* Write the phone number to edittext02 */
Medittext02.settext (numphone );
}
Else
{
SB. append ("no phone number found ");
}
/* Whether toast reads the complete phone type and phone number */
Toast. maketext (this, SB. tostring (),
Toast. length_short). Show ();
}
Catch (exception E)
{
/* Display the error message in textview */
Mtextview01.settext (E. tostring ());
E. printstacktrace ();
}
}
Break;
Default: break;
}
}
Catch (exception E)
{
E. printstacktrace ();
}
Super. onactivityresult (requestcode, resultcode, data );
}
}
-------------------------
String. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "app_name"> phonebook </string>
<String name = "str_button1"> Search </string>
<String name = "str_title"> my contacts </string>
<String name = "str_name"> name </string>
<String name = "str_telephone"> phone number </string>
</Resources>
-----------------------------
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Absolutelayout
Android: Id = "@ + ID/widget32"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
>
<Textview
Android: Id = "@ + ID/mytextview1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/str_title"
Android: layout_x = "0px"
Android: layout_y = "0px"
>
</Textview>
<Edittext
Android: Id = "@ + ID/myedittext01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/str_name"
Android: textsize = "18sp"
Android: layout_x = "0px"
Android: layout_y = "22px"
>
</Edittext>
<Edittext
Android: Id = "@ + ID/myedittext02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/str_telephone"
Android: textsize = "18sp"
Android: layout_x = "0px"
Android: layout_y = "82px"
>
</Edittext>
<Button
Android: Id = "@ + ID/mybutton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/str_button1"
Android: layout_x = "10px"
Android: layout_y = "142px"
>
</Button>
</Absolutelayout>
------------------------------------
Androidmanifest. xml
Note that after </Application>, add
<Uses-Permission
Android: Name = "android. Permission. read_contacts"> </uses-Permission>
<! -- Get the permission to read the address book -->