0

I am practising by making an app in which the user can send a WhatsApp message to a particular person .I tried some code snippets which i found on internet but whenever i try to send a WhatsApp msg from an actual device, i am gettig an error "No Application can perform this action".

Here is my code:-

public void sendMessage(View v) {
   try
   {
    String whatsAppMessage = message.getText().toString();
    Uri uri = Uri.parse("smsto:" + "9888873438");
    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
    i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
    i.setType("text/plain");
    i.setPackage("com.whatsapp");
    startActivity(Intent.createChooser(i, ""));
   }catch (Exception e) {
    Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
   }
}

Please help .

salil vishnu Kapur
  • 660
  • 1
  • 6
  • 29

1 Answers1

1

You receive no application can perform this action because you should remove i.setType("text/plain"); from your code:

String whatsAppMessage = message.getText().toString();
Uri uri = Uri.parse("smsto:" + "9888873438");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));

Unfortunately as you can see WhatsApp now opens in the conversation activity but there isn't the text you set in the Intent. This is because WhatsApp doesn't support this kind of share. The only supported share with Intent is ACTION_SEND as you can see in the WhatsApp FAQ:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94