在Android中获取收藏夹联系人
问题描述:
我想要获取Android联系人的收藏夹列表中的所有联系人。目前,我可以得到所有的组ID,包括最喜欢的组ID。但似乎没有联系人将组ID标识为最喜欢的组ID。在Android中获取收藏夹联系人
我试图让每组中的所有组ID和联系人。打印两个列表后,我发现,最喜欢的群组标识是不是在联系人列表中
ArrayList<String> favGroupId=new ArrayList<String>();
final String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor cursor = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
null, ContactsContract.Groups.TITLE);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Groups._ID));
Log.v("Test",id);
String gTitle = (cursor.getString(cursor
.getColumnIndex(ContactsContract.Groups.TITLE)));
Log.v("Test",gTitle);
if (gTitle.contains("Favorite_")) {
gTitle = "Favorites";
favGroupId.add(id);
}
}
cursor.close();
答
您可以使用在ContactsContract.Contact类的STARRED领域。如果你改变你的查询:
Cursor cursor = this.managedQuery(
ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",
new String[] {"1"}, null);
这应该返回出现在Android设备上的默认联系人应用程序收藏夹选项卡的所有联系人的列表。
答
完整的答案,包括intentUriString用于与意图打开联系人:
Map getFavoriteContacts(){
Map contactMap = new HashMap();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = managedQuery(queryUri, projection, selection, null, null);
while (cursor.moveToNext()) {
String contactID = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
String intentUriString = intent.toUri(0);
String title = (cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contactMap.put(title,intentUriString);
}
cursor.close();
return contactMap;
}