我无法访问联系人的联系电话。 我从互联网上获得了这个代码
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == PICK_CONTACT)
{
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();
}
但它只显示联系人的姓名。 我想要联系人的电话号码。
答案 0 :(得分:2)
查看此帖子,它有您想要的答案:Read all contact's phone numbers in android
答案 1 :(得分:0)
修改您的代码段
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts._ID);
startActivityForResult(intent, PICK_CONTACT);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{ if (requestCode == PICK_CONTACT)
{ Cursor cursor = managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + intent.getData(), null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(Email._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(Email.DATA1));
Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
// phone numbers
int _contact_id=10111; // your contact id
Cursor curPhone = null;
// 1 = home ,2= personal(mobile), 3= work
try {
Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] PROJECTION_PHONE = new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.CommonDataKinds.Phone.TYPE};
String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
String[] SELECTION_ARRAY_PHONE = new String[]{String.valueOf(_contact_id)};
curPhone = context.getContentResolver()
.query(URI_PHONE, PROJECTION_PHONE, SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
if (curPhone.getCount() > 0) {
curPhone.moveToFirst();
int indexNumber = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int labelIndex = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
int indexPhoneType = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
while (!curPhone.isAfterLast()) {
String contactNumber = curPhone.getString(indexNumber);
String labelName = "";
int labelType = curPhone.getInt(indexPhoneType);
if (labelType ==ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) {
labelName = curPhone.getString(labelIndex);
}
android.util.Log.e("!_@)(@_Number:- ", contactNumber + "[" + labelType + "] " + labelName);
curPhone.moveToNext();
}
} else {
// noPhones = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (curPhone != null) {
curPhone.close();
}
}
答案 3 :(得分:0)
要从联系人列表中获取所选联系人的联系人姓名,电话号码和电子邮件,只需添加此代码即可。
private Button btn;
private String TAG = "Contacts";
private static final int RESULT_PICK_CONTACT = 1;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectSingleContact();
}
});
private void selectSingleContact() {
Intent contactPickerIntent = new
Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForResult
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("ContactFragment", "Failed to pick contact");
}
}
private void contactPicked(Intent data) {
Uri uri = data.getData();
Log.i(TAG, "contactPicked() uri " + uri.toString());
Cursor cursor;
ContentResolver cr = getActivity().getContentResolver();
try {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (null != cur && cur.getCount() > 0) {
cur.moveToFirst();
for (String column : cur.getColumnNames()) {
Log.i(TAG, "contactPicked() Contacts column " + column + " : " + cur.getString(cur.getColumnIndex(column)));
}
}
if (cur.getCount() > 0) {
//Query the content uri
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (null != cursor && cursor.getCount() > 0) {
cursor.moveToFirst();
for (String column : cursor.getColumnNames()) {
Log.i(TAG, "contactPicked() uri column " + column + " : " + cursor.getString(cursor.getColumnIndex(column)));
}
}
cursor.moveToFirst();
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.i(TAG, "contactPicked() uri id " + id);
String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Log.i(TAG, "contactPicked() uri contact id " + contact_id);
// column index of the contact name
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// column index of the phone number
phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get Email id of selected contact....
Log.e("ContactsFragment", "::>> " + id + name + phoneNo);
Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null);
if (null != cur1 && cur1.getCount() > 0) {
cur1.moveToFirst();
for (String column : cur1.getColumnNames()) {
Log.i(TAG, "contactPicked() Email column " + column + " : " + cur1.getString(cur1.getColumnIndex(column)));
email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
//HERE YOU GET name, phoneno & email of selected contact from contactlist.....
Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
} else {
Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
}
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
同时为AndroidManifest.xml添加权限它对我有用,希望它也适合你。