Exercise code 3-2 secrets of Android Application Development
Source: Internet
Author: User
Public void oncreate (bundle savedinstancestate)
{
Textview TV = new textview (this );
String string = "";
Super. oncreate (savedinstancestate );
// Obtain the contentresolver object
Contentresolver Cr = getcontentresolver ();
// Obtain the cursor for the first entry in the phone book
Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, null );
While (cursor. movetonext ())
{
Try {
Int idcolumn = cursor. getcolumnindex (contactscontract. Contacts. _ id );
String contactid = cursor. getstring (idcolumn );
// Obtain the contact name
Int namefieldcolumnindex = cursor. getcolumnindex (phonelookup. display_name );
String contact = cursor. getstring (namefieldcolumnindex );
// Obtain the phone number
String phonenumber = "";
// Obtain the contact number
Int phonecount = cursor. getint (cursor. getcolumnindex (contactscontract. Contacts. has_phone_number ));
If (phonecount> 0 ){
// Obtain the contact's phone number
Cursor phones = getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri, null,
Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid,
Null, null );
If (phones. movetofirst ()){
Do {
// Traverse all phone numbers
Phonenumber + = phones. getstring (phones. getcolumnindex (
Contactscontract. commondatakinds. Phone. Number) + "\ t ";
System. Out. println (phonenumber );
} While (phones. movetonext ());
}
}
String + = Contact + ":" + phonenumber + "\ n ";
} Catch (exception ex ){
Ex. printstacktrace ();
}
// Cursor. movetonext ();
}
Cursor. Close ();
// Set the content displayed in textview
TV. settext (string );
// Display to the screen
Setcontentview (TV );
}
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