Android的 - 让“没有应用程序可以执行此操作”试图发送WhatsApp的消息的错误?应用程序、错误、消息、操作

2023-09-07 00:25:37 作者:少年不流氓:發育不正常

我通过一个应用程序中,用户可以发送WhatsApp的消息给特定的人修炼。我尝试了一些code片段,我互联网上找到,但每当我试图从实际的设备发送MSG的WhatsApp ,我gettig一个错误无应用程序可以执行此操作。

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".

下面是我的code: -

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

请帮忙。

推荐答案

您收到的没有应用程序可以执行此操作的,因为你应该删除 i.setType(text / plain的); 从code:

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

不幸的是,你可以看到,现在的WhatsApp在谈话活动打开,但没有你在意图设置文本。这是因为WhatsApp的不支持这种份额。意图唯一支持的份额为 ACTION_SEND ,你可以在的 WhatsApp的FAQ :

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