无法识别 WhatsApp 联系人
Unable to identify WhatsApp contacts
我正在尝试编写一个 Android 程序,该程序可以识别给定的联系电话号码是否与 WhatsApp 相关联。我设法查明特定联系人姓名是否有 WhatsApp 帐户。我如何找出与该联系人姓名对应的联系人有 WhatsApp?目前我正在使用以下代码:
public void identifyWhatsappContact() {
Cursor c = ctx.getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
RawContacts.ACCOUNT_TYPE + "= ? AND " + StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[] { "com.whatsapp", "John Doe" },
null);
ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
myWhatsappContacts.add(c.getString(contactNameColumn));
Log.v("WHATSAPP", c.getString(contactNameColumn)+"");
}
}
我能够确定 "John Doe" 是否有 WhatsApp,但我无法确定 "John Doe" 中有 phone 个有 WhatsApp(假设联系人有多个phone 个数字)。有人可以帮我吗?我已经尝试了解决方案 here,但它对我不起作用。
此示例提取联系人姓名的所有 WhatsApp 号码:
public void getWhatsAppNumbers(String contactName) {
Cursor cursor1 = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID},
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[]{"com.whatsapp", contactName},
null);
while (cursor1.moveToNext()) {
String rawContactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.RawContacts._ID));
Cursor cursor2 = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.DATA3},
ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
new String[]{"vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId},
null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(0);
if (TextUtils.isEmpty(phoneNumber))
continue;
if (phoneNumber.startsWith("Message "))
phoneNumber = phoneNumber.replace("Message ", "");
Log.d("whatsapp", String.format("%s - %s", contactName, phoneNumber));
}
}
}
我正在尝试编写一个 Android 程序,该程序可以识别给定的联系电话号码是否与 WhatsApp 相关联。我设法查明特定联系人姓名是否有 WhatsApp 帐户。我如何找出与该联系人姓名对应的联系人有 WhatsApp?目前我正在使用以下代码:
public void identifyWhatsappContact() {
Cursor c = ctx.getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
RawContacts.ACCOUNT_TYPE + "= ? AND " + StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[] { "com.whatsapp", "John Doe" },
null);
ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
myWhatsappContacts.add(c.getString(contactNameColumn));
Log.v("WHATSAPP", c.getString(contactNameColumn)+"");
}
}
我能够确定 "John Doe" 是否有 WhatsApp,但我无法确定 "John Doe" 中有 phone 个有 WhatsApp(假设联系人有多个phone 个数字)。有人可以帮我吗?我已经尝试了解决方案 here,但它对我不起作用。
此示例提取联系人姓名的所有 WhatsApp 号码:
public void getWhatsAppNumbers(String contactName) {
Cursor cursor1 = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID},
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[]{"com.whatsapp", contactName},
null);
while (cursor1.moveToNext()) {
String rawContactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.RawContacts._ID));
Cursor cursor2 = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.DATA3},
ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
new String[]{"vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId},
null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(0);
if (TextUtils.isEmpty(phoneNumber))
continue;
if (phoneNumber.startsWith("Message "))
phoneNumber = phoneNumber.replace("Message ", "");
Log.d("whatsapp", String.format("%s - %s", contactName, phoneNumber));
}
}
}