从联系人列表中获取 phone 个号码
get phone number from contactlist
我需要从联系人那里获取联系电话
我使用以下代码在 onCreate 中获取联系电话:
buttonSelectContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
并在 onAcivityResult 中使用以下代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
etName.setText(name);
etNumber.setText(number);
}
etName设置正确但etNumber错误,其中的值为0或1
如何在etNumber中设置联系电话??
这是我在 onActivityResult 中获取它的方式,它从片段中的联系人获取所有 phone 号码,因此如果您从 activity
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
textcontact.setText(name + "\n");
customerName = name;
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
textcontact.append(num + "\n");
phoneNumbers.add(num);
}
}
}
我需要从联系人那里获取联系电话
我使用以下代码在 onCreate 中获取联系电话:
buttonSelectContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
并在 onAcivityResult 中使用以下代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
etName.setText(name);
etNumber.setText(number);
}
etName设置正确但etNumber错误,其中的值为0或1 如何在etNumber中设置联系电话??
这是我在 onActivityResult 中获取它的方式,它从片段中的联系人获取所有 phone 号码,因此如果您从 activity
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
textcontact.setText(name + "\n");
customerName = name;
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
textcontact.append(num + "\n");
phoneNumbers.add(num);
}
}
}