In the vast en-SO found the answer , the comments say that the example is working. I try to do the same in my application, but on the first attempt it seems as if WhatsApp launched and immediately closes. In subsequent attempts, nothing happens at all, the only reaction is a string in the logs:

V / WhatsApp: Total WhatsApp Contacts: 17

I do it like this:

  Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = ContactUriHelper.getUriFromPhoneNumber(contact.getPhone().getNumber(), getApplicationContext()); intent.setDataAndType(Uri.parse("content://com.android.contacts/data/2021"), //Uri.parse("content://com.android.contacts/data/_id") "vnd.android.cursor.item/vnd.com.whatsapp.voip.call"); intent.setPackage("com.whatsapp"); startActivity(intent); 

Uri tried to transmit it in a string and by the uri object itself - nothing changed.

Uri I get as follows

  public static Uri getUriFromPhoneNumber(String phoneNumber, Context context) { Uri uri = null; String contactId = getContactIdByPhoneNumber(phoneNumber, context); if (!TextUtils.isEmpty(contactId)) { Cursor cursor = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data._ID}, ContactsContract.Data.DATA2 + "=? AND " + ContactsContract.Data.CONTACT_ID + " = ?", new String[]{"Viber", contactId}, null); if (cursor != null) { Log.wtf(TAG, cursor.toString()); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID)); if (!TextUtils.isEmpty(id)) { uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id); Log.d(TAG, "URI: " + uri.toString()); break; } } cursor.close(); } } return uri; } private static String getContactIdByPhoneNumber(String phoneNumber, Context context) { ContentResolver contentResolver = context.getContentResolver(); String contactId = null; Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); String[] projection = new String[]{ContactsContract.PhoneLookup._ID}; Cursor cursor = contentResolver.query( uri, projection, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID)); } cursor.close(); } Log.wtf(TAG, "ContactID: " + contactId); return contactId; } 

What could be the problem?

  • And id 2021 is in contacts whatsapp? - hardsky
  • What does whatsapp mean in contacts? I take it out of the phone book of the device, I add methods to the question with which I get Uri, and then I do uri.toString () and get a string which I then substitute in the WhatsApp call - Kirill Stoianov
  • the answer link you give mentions that "only those _Ids whose MIME type is vnd.android.cursor.item / vnd.com.whatsapp.voip.call" - hardsky
  • can be more specific, what needs to be done, what am I doing wrong? - Kirill Stoianov

1 answer 1

The link says that you need to filter the records by mimetype "vnd.android.cursor.item/vnd.com.whatsapp.voip.call"
those.

 public static Uri getUriFromPhoneNumber(String phoneNumber, Context context) { Uri uri = null; String contactId = getContactIdByPhoneNumber(phoneNumber, context); if (!TextUtils.isEmpty(contactId)) { Cursor cursor = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data._ID }, ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.CONTACT_ID + " = ?", new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.voip.call", contactId }, null); if (cursor != null) { Log.wtf(TAG, cursor.toString()); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID)); if (!TextUtils.isEmpty(id)) { uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id); Log.d(TAG, "URI: " + uri.toString()); break; } } cursor.close(); } } return uri; } 
  • I take Uri for Viber and not for WhatsApp. It all worked! - Kirill Stoianov
  • one
    @ KirillStoianov, most of the errors on attention :) - hardsky