Send a text message from my android application to Whatsapp to specific Contact
Asked Answered
M

3

3

I'm trying to send a text message from my android application to Whatsapp to specific Contact. when I'm using below codes, I am succeed either to send message and have to pickup contact manually, or If Specific number chat window opens, but message is blank. So is it possible to do both with one intent ? Here is my code:

  1. I can share message to WhatsApp, but contact i have 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();        
    }
    
  2. Specific number in wats app window opens, but message is blank:

    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);
    
Message answered 26/2, 2016 at 9:18 Comment(0)
C
0

Below code will helps you :

 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);
Counterstatement answered 26/2, 2016 at 9:25 Comment(1)
Thank you for quick response, But i am not getting wats app option to send message. Please refer below screenshot: !Valid XHTML.Message
D
0

This is what works for me.

The parameter 'body' gets not read by the whatsapp app, use 'Intent.EXTRA_TEXT' instead.

By setting the '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);
Dustup answered 15/1, 2017 at 19:53 Comment(1)
By your code, Specific number in wats app window opens, but message is blankMessage
B
0

I use this:

String url = "https://api.whatsapp.com/send?phone=" + phoneNo + "&text=" + message;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
intent.setPackage(packageName);
    
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    System.out.println("Error Message");
}
Borscht answered 18/4, 2023 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.