How to get the contacts that are used in whatsapp or another application in android

Hi, I want to get the contact that is used by another application (for example, whatsapp or viber) see image below

enter image description here

please help me in this matter thanks

+7
android whatsapp contacts viber
source share
2 answers

With READ_CONTACTS permission in your manifest, you can get synchronized contacts based on the type of account. For WhatsApp, this is "com.whatsapp" . So:

 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)); } 
+24
source share

myWhatsappContacts ArrayList will contain all the phone numbers that are present in your whatsapp application.

 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))); } 
0
source share

All Articles