I looked through similar topics, but did not solve the problem. The window with WhatsApp will open only after 2 clicks on the button, the first time you press, only the number is recorded in the contacts, if it is not there. When debugging, when I first pressed, I found that I could not enter the while block, since cursor.moveToNext () is always false when you first click on the getUriFromPhoneNumber method, and when I click 2 times and all subsequent times - it works fine, please tell me where I could be wrong.
public static void callWhatsApp(String phoneNumber, String name, Activity activity) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = getUriFromPhoneNumber(phoneNumber, activity.getApplicationContext(), name, activity); intent.setDataAndType(uri, "vnd.android.cursor.item/vnd.com.whatsapp.voip.call"); intent.setPackage("com.whatsapp"); activity.startActivity(intent); } private static Uri getUriFromPhoneNumber(String phoneNumber, Context context, String name, Activity activity) { Uri uri = null; String contactId = getContactIdByPhoneNumber(phoneNumber, context); // check for exist phone number 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) { // here in the first time equals false and not will work, need fix while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID)); if (!TextUtils.isEmpty(id)) { uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id); break; } } cursor.close(); } } else { addToContactsNewNumber(phoneNumber, name, activity); } return uri; } // find phone number in contacts 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(); } return contactId; } private static void addToContactsNewNumber(String number, String name, Activity activity) { ArrayList<ContentProviderOperation> ops = new ArrayList<>(); int rawContactInsertIndex = ops.size(); ops.add(ContentProviderOperation.newInsert( ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build()); ops.add(ContentProviderOperation.newInsert( ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name).build()); ops.add(ContentProviderOperation. newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) .build()); try { activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); callWhatsApp(number, name, activity); } catch (RemoteException | OperationApplicationException e) { e.printStackTrace(); } } No way has helped, I am already losing hope ...
activity.getApplicationContext()- the context of the application sometimes gives strange tricks. Try justactivityas context - Barmaleycursor.moveToFirst();before the cycle. - Yuriy SPb ♦