In the development of android applications, it is often necessary to call the contact interface that comes with the system. After selecting a person, you can obtain the corresponding name and number and return it.
There are also some differences in implementation methods before and after android2.0: The main difference is that the contact API of Version 2.0 and later has changed from the original Contacts to ContactsContract.
(1) how to retrieve the contact list before 2.0:
Method 1
Intent intent = new Intent ();
Intent. setAction (Intent. ACTION_PICK );
Intent. setData (Contacts. People. CONTENT_URI );
StartActivityForResult (intent,
PICK_CONTACT
);
Method 2
Intent intent = new Intent (Intent. ACTION_PICK );
Intent. setType ("Contacts. People. CONTENT_TYPE"); // vnd. android. cursor. dir/person
StartActivityForResult (intent,
PICK_CONTACT
);
OnActivityResult
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode ){
Case
PICK_CONTACT
:
If (data = null ){
Return;
}
Uri uri = data. getData ();
Cursor cursor = getContentResolver (). query (uri, null );
Cursor. moveToFirst ();
String number = cursor. getString (cursor. getColumnIndexOrThrow (Phones. NUMBER ));
Log. d (TAG, "number" + number );
MContactText. setText (number );
MContactText. setSelection (number. length ());
Break;
Default:
Break;
}
}
(2) how to retrieve the contact list after 2.0:
Method 1
Intent intent = new Intent (Intent. ACTION_PICK, ContactsContract. Contacts. CONTENT_URI );
StartActivityForResult (intent, PICK_CONTACT );
Method 2
Intent intent = new Intent (Intent. ACTION_PICK );
Intent. setType (ContactsContract. Contacts. CONTENT_TYPE); // vnd. android. cursor. dir/contact
StartActivityForResult (intent, PICK_CONTACT );
OnActivityResult
@ Override
Public void onActivityResult (int reqCode, int resultCode, Intent data ){
Super. onActivityResult (reqCode, resultCode, data );
Switch (reqCode ){
Case (PICK_CONTACT ):
If (resultCode = Activity. RESULT_ OK ){
Uri contactData = data. getData ();
Cursor c = managedQuery (contactData, null );
If (c. moveToFirst ()){
String name = c. getString (c. getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME ));
String hasPhone = cursor. getString (cursor. getColumnIndex (ContactsContract. Contacts. HAS_PHONE_NUMBER ));
String phoneNumber = null;
If (hasPhone. inclusignorecase ("1 ")){
HasPhone = "true ";
} Else {
HasPhone = "false ";
}
If (Boolean. parseBoolean (hasPhone )){
Cursor phones = getContentResolver (). query (ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null, ContactsContract. example. Phone. CONTACT_ID + "=" + contactId, null, null );
While (phones. moveToNext ()){
PhoneNumber = phones. getString (phones. getColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));
}
Phones. close ();
}
}
}
Break;
}
}
From a lonely sharer