Send a text message from my Android app to Whatsapp for a specific contact

I am trying to send a text message from an Android application to Whatsapp for a specific contact. when I use the codes below, I can either send a message, or manually connect the contact, or open the chat window of a specific number, but the message is empty. So is it possible to do both with the same intention? Here is my code:

  • I can share the message with WhatsApp, but contact him to choose manually:

    Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.setPackage("com.whatsapp"); i.putExtra(Intent.EXTRA_TEXT, "Hello World"); try { activity.startActivity(i); } catch (Exception e) { e.printStackTrace(); } 
  • A specific number will open in the wats application window, but the message is empty:

     Uri uri = Uri.parse("smsto:" + number); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra("sms_body", "smsText"); i.setPackage("com.whatsapp"); activity.startActivity(i); 
+3
source share
2 answers

Below is the code below:

  String strMessageToShare=YourEditText.getText().tostring(); final Intent myIntent = new Intent( android.content.Intent.ACTION_SEND_MULTIPLE); myIntent.setType("text/plain"); myIntent.putExtra(android.content.Intent.EXTRA_TEXT, new String[]{strMessageToShare}); YourActivity.this.startActivityForResult(Intent .createChooser(myIntent, "Sharing message..."), 1); 
0
source

This is what works for me.

The body parameter is not read by whatsapp, use Intent.EXTRA_TEXT instead.

By setting "phoneNumber", you specify the contact to open in whatsapp.

  Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage)); sendIntent.putExtra(Intent.EXTRA_TEXT, message); sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); 
0
source

Source: https://habr.com/ru/post/972326/


All Articles