Mayura Savaliya
Mayura Savaliya

Reputation: 440

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

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);
    

Upvotes: 3

Views: 1853

Answers (3)

S. Gabran
S. Gabran

Reputation: 11

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");
}

Upvotes: 0

Yoraco Gonzales
Yoraco Gonzales

Reputation: 737

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);

Upvotes: 0

user5716019
user5716019

Reputation: 598

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);

Upvotes: 0

Related Questions