如何从android中的电话簿中检索联系人号码?
/这是我的覆盖功能。当我从电话簿中检索名字时,它工作正常。但是,当我尝试检索联系人号码时,该应用崩溃了。/如何从android中的电话簿中检索联系人号码?
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
phones.moveToFirst();
String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
}
}
c.close();
}
break;
}
}
我不知道你有什么问题,但我对获得的电话号码,你可以试试代码:
所有的public String getPhone(Context context, String contactID) {
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
// Query and loop for every phone number of the contact
Cursor phoneCursor = context.getContentResolver().query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contactID}, null);
String phoneNo = "";
if(phoneCursor != null) {
while (phoneCursor.moveToNext()) {
phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
}
phoneCursor.close();
}
return phoneNo;
}
首先,创建POJO类。
public class ContactEntity {
String name;
String number;
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
现在,检索联系人。
ArrayList<ContactEntity> contactList = new ArrayList<>();
contactList = getcontactList();
public ArrayList<ContactEntity> getcontactList() //This Context parameter is nothing but your Activity class's Context
{
ArrayList<ContactEntity> allContacts = new ArrayList<>();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
if (contactsCount > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//the below cursor will give you details for multiple contacts
Cursor pCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
// continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
while (pCursor.moveToNext()) {
int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
//String isStarred = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//you will get all phone numbers according to it's type as below switch case.
ContactEntity entity = new ContactEntity();
entity.setName(contactName);
entity.setNumber(phoneNo);
allContacts.add(entity);
//Log.e will print all contacts according to contact types. Here you go.
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
break;
default:
break;
}
}
pCursor.close();
}
}
cursor.close();
}
return allContacts;
}
我一直在这一行上发生错误 - getContentResolver()。query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);你会知道为什么吗? –
您是否为清单文件中的联系人提供了权限.. @Gerard Grundy –
是的,我将其添加到清单。 –
张贴您的错误logcat。 –
你有什么错误?你应该发布你的整个错误 – BlackHatSamurai
我得到运行时错误: –