我可以选择姓名,但是当名字中有超过1个电话号码时,我很困惑如何选择电话号码。以下是我的代码。
public class getName extends Activity {
//permission
//<uses-permission android:name="android.permission.READ_CONTACTS" />
/** Called when the activity is first created. */
private static final int PICK_CONTACT = 0;
EditText Name;
EditText Phone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Name = (EditText)findViewById(R.id.editText1);
Phone = (EditText)findViewById(R.id.editText2);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// to read contacts add android.permission.READ_CONTACTS
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
//intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
ContentResolver cr = getContentResolver();
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.Phones.NAME));
Name.setText(name);
Uri uri = data.getData();
Cursor cursor=managedQuery(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
}
}
break;
}
}
}
答案 0 :(得分:0)
如果取消注释onClick按钮中的第二行,您应该会看到一个包含联系人姓名及其编号的列表。那么,你可以直接选择一个数字
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);