您可以有意地从电话簿中选择一个联系人:
private void openPhonebook()
{
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
pickContactIntent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
您应该在onActivityResult() 中处理响应意图:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if ( requestCode == PICK_CONTACT_REQUEST )
{
if ( resultCode == RESULT_OK )
{
Uri contactUri = intent.getData();
String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
String phone = cursor.getString(column);
column = cursor.getColumnIndex(Phone.DISPLAY_NAME);
String name = cursor.getString(column);
//set phone and to EditText
etName.setText(name.trim());
petPhone.setText(phone);
cursor.close();
}
}
}
还有权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />