嗨,我想获得其他应用程序(如whatsapp或viber)使用的联系方式,请参见下图
请帮我解决这个问题谢谢
通过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));
}
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)));
}
System.out.print("姓名 : "+cursor.getString(contactNameColumn) +"\n "+" 电话号码" + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));