我使用以下代码,我得到同一个人的多个姓名和电话号码。如何才能获得我拨打过的所有用户的单个姓名和手机号码。
代码:
if (android.os.Build.VERSION.SDK_INT >= 21) {
mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP + " DESC");
}else{
mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.LAST_TIME_CONTACTED + " DESC");
}
int number = mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
while (mCursor.moveToNext()) {
String phName = mCursor.getString(name);
String phNumber = mCursor.getString(number);
}
}
更新代码:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.LABEL},
ContactsContract.Data.CONTACT_ID + "=?" + " AND "
+ ContactsContract.Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
new String[]{String.valueOf(id)}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("num", "Name: " + name + ", Phone No: " + phoneNo);
}
pCur.close();
}
}
}
答案 0 :(得分:0)
您应该使用以下特定字段来代替manageQuery,并将其保留在SELECT查询中。以下只是示例,您可以根据您的要求参考和使用:
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
new String[] {String.valueOf(contactId)}, null);