0

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:

  1. 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);
    
  2. 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?

Fernando
  • 751
  • 2
  • 13
  • 27
  • Here is a similar question like yours http://stackoverflow.com/a/15931345/2455351 – migue02 Dec 10 '14 at 14:37
  • That's the same as my above option 1. The difference is this guy uses pacjagemanager class to check if whatsapp is installed or not. – Fernando Dec 10 '14 at 18:30

0 Answers0