3
votes

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

2 Answers

0
votes

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

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