我正在练习制作一个应用程序,用户可以在其中向特定的人发送 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/