0

I have this code. It is only opening WhatsApp with a particular number but not with text.

Uri uri = Uri.parse("smsto:" +mobilenumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Hello StackOverFlow");
i.putExtra("chat",true);
i.setPackage("com.whatsapp");
startActivity(i);

I have tried using Intent.EXTRA_TEXT but no result.

How to pass the text?

Andrew T.
  • 4,637
  • 8
  • 40
  • 60
sandeep
  • 89
  • 9

1 Answers1

1

You can not directly send message to specific contact in whatsapp from your code. you can pass text to listing screen then user can choose user to send the message.

Try below code:

 final Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
 whatsappIntent.setPackage("com.whatsapp");
 whatsappIntent.putExtra(Intent.EXTRA_TEXT, text);
 whatsappIntent.setType("text/plain");
 try {
       mContext.startActivity(whatsappIntent);
 } catch (ActivityNotFoundException ex) {
       ex.printStackTrace();
       Toast.makeText(mContext, "WhatsApp is not installed.", Toast.LENGTH_SHORT).show();
 }

EDIT:

you can not Send Whatsapp message to specific contact. Refer Send Whatsapp message to specific contact

I hope it helps!

Community
  • 1
  • 1
Rajesh
  • 12,711
  • 5
  • 49
  • 77
  • Thanks for reply. i am able to open whatsapp chat for that particular number with my code. only thing i need to send the text as i am sending number . – sandeep Aug 18 '15 at 08:52
  • it's for sending message without opening whatsapp – sandeep Aug 18 '15 at 08:57
  • Please Look at my Question.i want to send to particular number only – sandeep Aug 18 '15 at 09:00
  • 1
    Yes i know your question but whasapp does not allow to directly send message to specific number. – Rajesh Aug 18 '15 at 09:05
  • the code you have provided. it only opens particular contact chat screen but does not pass any message because whasapp is not allowing it – Rajesh Aug 18 '15 at 09:07