I've been looking at other answers in this forum but i didn't find a complete one. I wanna send a message through whatsapp to an specific number. Say this number is 123456789.
The two possibilities i've seen are these ones:
The problem here is that you have to choose to whom you wanna send the messsage:
Intent whatsapp = new Intent(Intent.ACTION_SEND); whatsapp.setPackage("com.whatsapp"); whatsapp.setType("text/plain"); whatsapp.putExtra(Intent.EXTRA_TEXT, "Hello World"); startActivity(whatsapp);
In this case, the user is selected correctly but the text isn't added:
String whatsappid = "123456789@s.whatsapp.net"; Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?", new String[] { whatsappid }, null); c.moveToFirst(); Intent whatsapp = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0))); c.close(); whatsapp.putExtra(Intent.EXTRA_TEXT, "Hello World"); startActivity(whatsapp);
When I try to mix both solutions either the ActivityNotFoundException is thrown (if i set the whatsapp as the package to use) or the wrong application is launched (in case i don't use ACTION_SEND). I've tried with ACTION_SENDTO and it doesn't work either.
I know someone is gonna mark it as duplicated... Anyway, I'll take the risk.
May anyone help me?