您好我正在尝试使用我正在处理的应用程序实现WhatsApp。我希望能够显示用户拥有的所有WhatsApp联系人以及他们的电话号码。到目前为止,我已经能够获取他们的WhatsApp联系人的名称,但我无法获取他们的WhatsApp联系人的电话号码,这是我用来获取WhatsApp用户联系人列表的代码。
cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {
ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY
}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[] {
"com.whatsapp"
}, null);
int contactNameColumn = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
while (cursor.moveToNext()) {
String name = cursor.getString(contactNameColumn);
System.out.println(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
Integer.toString(contactNameColumn)
}, null);
while (pCur.moveToNext()) {
String phonenumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("PHONE: " + phonenumber);
}
pCur.close();
}
cursor.close();
当我打印出名称时,它会正确打印出来,然后我使用该ID查询数据表以获取他们的电话号码,但总是返回null
,任何人都可以帮我解决。
由于
答案 0 :(得分:0)
像这样更改pCur
if (name != null) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{name}, null);
//Iterate cursor here...
}
最好对变量使用适当的命名约定。为什么因为这里变量name
代表了conatact的id。
答案 1 :(得分:0)
你可以阅读所有拥有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<Int> 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. String phonenumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
myWhatsappContacts.add(phonenumber);
}
答案 2 :(得分:0)
使用以下代码从电话簿获取WhatsApp联系人以及相关的whatsApp号码:
private void displayWhatsAppContacts() {
final String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE,
"account_type",
ContactsContract.Data.DATA3,
};
final String selection = ContactsContract.Data.MIMETYPE + " =? and account_type=?";
final String[] selectionArgs = {
"vnd.android.cursor.item/vnd.com.whatsapp.profile",
"com.whatsapp"
};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(
ContactsContract.Data.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
String number = c.getString(c.getColumnIndex(ContactsContract.Data.DATA3));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("WhatsApp", "name " +name + " - number - "+number);
}
Log.v("WhatsApp", "Total WhatsApp Contacts: " + c.getCount());
c.close();
}