1

I'm using this code in order to send a message to a specific number in WhatsApp:

    public void sendMessage() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            String whatsappid = "962795195996@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, "Test...");

            whatsapp.setPackage("com.whatsapp");

            try {
                startActivity(whatsapp);
            } catch (android.content.ActivityNotFoundException ex) {
                ex.printStackTrace();
            }
        }
    });
}

When running the code, WhatsApp is opened on the message history with that number but the text message is not filled in the input box. How can this be solved?

jww
  • 97,681
  • 90
  • 411
  • 885
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54

1 Answers1

0

Change Intent.ACTION_VIEW to Intent.ACTION_SEND

and add whatsapp.setType("text/plain");

  • I modified my code like what you said, but instead of opening the conversation page, now opens the list of contacts to choose a contact to send message to, then it's sent. Any ideas? – Hamzeh Soboh Jan 22 '15 at 08:37