Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share text in what's app on particular Number

Using this code only open particulat number's chat but Text is not share.How can I do this?

public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (EditText)findViewById(R.id.editText);
    Wa = (Button)findViewById(R.id.btn_whatsapp);
    Wa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("smsto:" + id);
            Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);

            String text = "testing message";
            waIntent.setPackage("com.whatsapp");
            if (waIntent != null) {
                waIntent.putExtra(Intent.EXTRA_TEXT, text);
                startActivity(Intent.createChooser(waIntent, text));
            } else {
                Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
                        .show();
            }

         }
    });

}
like image 317
GB_Bhayani ツ Avatar asked Feb 06 '16 06:02

GB_Bhayani ツ


People also ask

How can I send a message to a number without adding them on WhatsApp?

This method works for both Android and iOS. All you need to is follow a few simple steps on any browser and you're good to go. Open your phone's browser. Now you can copy and paste this link http://wa.me/xxxxxxxxxx, or this link — http://api.whatsapp.com/send?phone=xxxxxxxxxx in the address bar.


1 Answers

Since you trying to achieve it as "smsto:", "text/plain" as type will help you. Try Extra as "sms_body" if it won't help.

Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
    waIntent.setType("text/plain");
    //waIntent.putExtra(Intent.EXTRA_TEXT, text);
    waIntent.putExtra("sms_body", text); 
    startActivity(Intent.createChooser(waIntent, text));
} else {
    Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
            .show();
}
like image 136
Let'sRefactor Avatar answered Nov 06 '22 20:11

Let'sRefactor