[英]Can get ID from received phone number, but not group
我已經成功地從正在撥打的電話號碼中獲取ID和名稱。我想要查看的是該ID屬於哪個組。 我嘗試了以下方法:
//Search for the information about the phone number, save the goupID(s)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(aNumber));
ContentResolver cr = mService.getContentResolver();
Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);
myCursor.moveToFirst();
//String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID));
String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID));
myCursor.close();
//Use the cursor to query for group with help of ID from the Phone look up
myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
new String[]{ContactsContract.Groups._ID},
ContactsContract.Groups._ID + " = ?",
new String[]{contactID},
null);
//Contact may be in more than one group
nbrOfGroups = myCursor.getCount();
groupName = new String [nbrOfGroups];
問題是第二個查詢,我想使用在電話查找中找到的contactID來查看contacID屬於哪個組。 盡管聯系人已添加到我的聯系人中的一個組,但結果是沒有任何組。
有任何想法嗎? :)
Groups._ID與聯系人ID不同,它是存儲所有組信息的表的索引。 獲得聯系人ID之后,您應該使用“組成員身份” mimetype從數據表中獲取該聯系人的所有組成員身份。
獲取組ID后,您可以查詢“組”表以獲取所有組的TITLE
嘗試使用此代碼
//Search for the information about the phone number, save the goupID(s)
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123123"));
ContentResolver cr = this.getContentResolver();
Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);
myCursor.moveToFirst();
//String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID));
String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID));
myCursor.close();
//Use the cursor to query for group with help of contact ID from the Phone look up
myCursor = cr.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.CONTACT_ID,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},
ContactsContract.Data.CONTACT_ID + " = ? " +
Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
new String[]{contactID},
null);
//Contact may be in more than one group
int nbrOfGroups = myCursor.getCount();
int[] groupIds = new int[nbrOfGroups];
int index = 0;
// unfortunately group names are stored in Groups table
// so we need to query again
if (myCursor.moveToFirst()) {
do {
groupIds[index] = myCursor.getInt(1); // Group_row_id column
} while (myCursor.moveToNext());
}
myCursor.close();
// construct the selection
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nbrOfGroups; i++) {
if (i != 0) {
sb.append(",");
}
sb.append(groupIds[i]);
}
String[] groupName = new String [nbrOfGroups];
myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
new String[]{ContactsContract.Groups.TITLE},
ContactsContract.Groups._ID + " IN (" + sb.toString() + ")",
null, null);
// finally got the names
if (myCursor.moveToFirst()) {
do {
groupName[index] = myCursor.getString(0); // Group_row_id column
} while (myCursor.moveToNext());
}
myCursor.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.