Usecontentprovidergetcontactsactivity. Java
Package gogler. myandroid;
Import Android. App. activity;
Import Android. content. contentresolver;
Import Android. database. cursor;
Import Android. OS. Bundle;
Import Android. provider. contactscontract;
Import Android. provider. contactscontract. phonelookup;
Import Android. widget. textview;
Public class usecontentprovidergetcontactsactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Textview TV = new textview (this );
String string = "contacts: \ n ";
String number = "";
/* 1. Obtain contentresolver */
Contentresolver Cr = This. getcontentresolver ();
/* 2. Obtain a cursor query object */
Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, null,
Null, null, null );
/* 3. Traverse contentresolver */
While (cursor. movetonext ()){
/*
* Retrieve the contact name index
*/
Int namefieldcolumnindex = cursor
. Getcolumnindex (phonelookup. display_name );
/* Get the contact name */
String contact = cursor. getstring (namefieldcolumnindex );
/* 4. Create a cursor */
Cursor target = This. getcontentresolver (). Query (
Contactscontract. Contacts. content_uri, null, null,
Null );
/* 5. Locate the cursor to the first line */
Target. movetofirst ();
/* 6. Obtain the contact ID */
String contentid = cursor. getstring (Target
. Getcolumnindex (contactscontract. Contacts. _ id ));
/* 7. Check whether the current contact has a mobile phone number */
String ispnone = cursor
. Getstring (cursor
. Getcolumnindex (contactscontract. Contacts. has_phone_number ));
/* 8. If the current contact has a phone number */
If (integer. parseint (ispnone)> 0 ){
/* 9. Create a cursor-contact ID */
Cursor phone = This. getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri,
Null,
Contactscontract. commondatakinds. Phone. contact_id + "="
+ Contentid, null, null );
/* 10. Retrieve multiple mobile phone numbers of the current contact */
While (phone. movetonext ()){
// Obtain the contact number
Number = phone
. Getstring (Phone
. Getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
String + = (contact + ":" + number + "\ n ");
}
}
}
TV. settext (string );
Setcontentview (TV );
}
}
Manifest. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "gogler. myandroid"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "13"/>
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
<Application
Android: icon = "@ drawable/Russia"
Android: Label = "@ string/app_name">
<Activity
Android: Label = "@ string/app_name"
Android: Name = ". usecontentprovidergetcontactsactivity">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>
Code Packaging
Http://download.csdn.net/download/gogler/3725437
Demo