Android how to get contact list which used whats app application programmatically

孤者浪人 提交于 2019-12-06 07:56:53

问题


i have working with small android application in this application i am try to get contact which used whats app application and also alert for my selected whats app contact from my application when contact updates his/her profile picture and status.


回答1:


You can query your content cursor to see what properties contacts have.

Cursor c1 = appActivity.getContentResolver().query(
                     ContactsContract.Data.CONTENT_URI
                     ,null,null,null, null);
c1.moveToFirst();
DatabaseUtils.dumpCursor(c1);
c1.close();

Or specifically if you want to query for whatsapp contacts here are the properties:

  • You can query for ContactsContract.RawContacts.ACCOUNT_TYPE with value com.whatsapp
  • You can use MIMETYPE with value vnd.android.cursor.item/vnd.com.whatsapp.profile

Example:

 c = appActivity.getContentResolver().query(
             ContactsContract.Data.CONTENT_URI
             ,new String[] { ContactsContract.Contacts.Data._ID }
            ,"mimetype=?",
             new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.profile" }, null);

c1.moveToFirst();
DatabaseUtils.dumpCursor(c1);
c1.close();

Note (@Ragnar): MIMETYPE column didn't work for me. I used ACCOUNT_TYPE column and it worked.




回答2:


If you wish read Telegram contacts:

private val tgContactMimeType = "vnd.android.cursor.item/vnd.org.telegram.messenger.android.profile";

private val projectionTelegram = arrayOf(
    ContactsContract.Data.DATA1,//userId
    ContactsContract.Data.DATA3,//userPhone
    ContactsContract.Data.DISPLAY_NAME)//displayName

//read telegram contacts
phoneCursor = cr.query(
    ContactsContract.Data.CONTENT_URI,
    projectionTelegram,
    ContactsContract.Data.MIMETYPE + " = '" + tgContactMimeType + "'",
    null,
    null)
if (phoneCursor != null) {
    while (phoneCursor.moveToNext()) {
        val userId = phoneCursor.getString(0)
        val number = phoneCursor.getString(1)
        val displayname = phoneCursor.getString(2)
        val contact = PhoneContact()
        contact.firstName = displayname
        contact.phones.add(number)
        contactsMap[userId.toString()] = contact
    }
}


来源:https://stackoverflow.com/questions/24227356/android-how-to-get-contact-list-which-used-whats-app-application-programmaticall

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!