您好我想获得其他应用程序使用的联系人(如whatsapp或viber) 请参见下面的图片
请帮我解决这个问题 感谢
答案 0 :(得分:24)
使用您的清单中的READ_CONTACTS
权限,您可以根据帐户类型获得已同步的联系人。对于WhatsApp,它是"com.whatsapp"
。所以:
Cursor c = getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
RawContacts.ACCOUNT_TYPE + "= ?",
new String[] { "com.whatsapp" },
null);
ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
// You can also read RawContacts.CONTACT_ID to read the
// ContactsContract.Contacts table or any of the other related ones.
myWhatsappContacts.add(c.getString(contactNameColumn));
}
答案 1 :(得分:0)
myWhatsappContacts ArrayList将包含whatsapp应用程序中存在的所有电话号码。
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI
,new String[] {ContactsContract.Data._ID
,ContactsContract.Data.DISPLAY_NAME
,ContactsContract.CommonDataKinds.Phone.NUMBER
,ContactsContract.CommonDataKinds.Phone.TYPE}
,ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "' AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?"
,new String[] { "com.whatsapp" }
, null);
while (cursor.moveToNext())
{
myWhatsappContacts.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
答案 2 :(得分:0)
System.out.print(“ Name:” + cursor.getString(contactNameColumn)+“ \ n” +“电话号码” + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));