Andriod reads the address book, andriod address book
Package com. example. yanlei. wifi;
Import android. content. ContentResolver;
Import android. database. Cursor;
Import android. OS. Bundle;
Import android. provider. ContactsContract;
Import android. provider. ContactsContract. PhoneLookup;
Import android. support. v7.app. AppCompatActivity;
Import android. util. Log;
Import android. widget. Button;
Import android. widget. ListView;
Import android. widget. TextView;
Public class MainActivity extends AppCompatActivity {
Private ListView lvContacts = null;
Private Cursor cursor = null;
Private Button btnRead = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TextView TV = new TextView (this );
String str = "";
// Obtain the contentresolver object
ContentResolver cr = getContentResolver ();
// Obtain the cursor of the Start item in the phone book. You must first moveToNext ()
Cursor cursor = cr. query (ContactsContract. Contacts. CONTENT_URI, null,
Null, null, null );
While (cursor. moveToNext ())
{
// Retrieve the contact name index
Int nameIndex = cursor. getColumnIndex (PhoneLookup. DISPLAY_NAME );
String contact = cursor. getString (nameIndex );
Str + = (contact + ":" + "\ n ");
// Obtain the contact's ID index value
String contactId = cursor. getString (cursor
. GetColumnIndex (ContactsContract. Contacts. _ ID ));
Cursor phone = cr. query (
ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null,
ContactsContract. CommonDataKinds. Phone. CONTACT_ID + "="
+ ContactId, null, null );
// A person may have several numbers
While (phone. moveToNext ())
{
String strPhoneNumber = phone
. GetString (phone
. GetColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));
Str + = (strPhoneNumber + "\ n ");
Log. I ("TAG", "phoneNumber:" + strPhoneNumber + "contact" + contact );
}
Phone. close ();
}
Cursor. close ();
TV. setText (str );
SetContentView (TV );
}
}