java - Android - 尝试发送 Whatsapp 消息时出现错误 “no application can perform this action”?

标签 java android

我正在练习制作一个应用程序,用户可以在其中向特定的人发送 WhatsApp 消息。我尝试了一些在互联网上找到的代码 fragment ,但每当我尝试从实际设备发送 WhatsApp 消息时,我出现错误“没有应用程序可以执行此操作”。

这是我的代码:-

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

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 现在在对话 Activity 中打开,但没有您在 Intent 中设置的文本。这是因为 WhatsApp 不支持这种共享。 Intent 唯一支持的共享是 ACTION_SEND,如您在 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);

关于java - Android - 尝试发送 Whatsapp 消息时出现错误 “no application can perform this action”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30036992/

相关文章:

java - 在服务器上实现作业计时器?

java - Dos命令获取IE页面源

java - 数组的并发问题(Java)

java - 创建子类时是否需要 super ?

java - 抛出和捕获 IOException

android - 如何在服务器上发送变量 float ?

android - 无法将 Android 应用程序安装为模拟器上的系统应用程序以通过 WRITE_APN_SETTINGS 的 SecurityException

java - 无法应用插件 "com.google.gms.google-services"

android - 如何为 GridLayoutManager 使用 ItemDecoration

android - 找不到 R.Id。在 fragment 中