Android – 在尝试发送whatsapp消息时收到错误“没有应用程序可以执行此操作”?

我正在练习制作一个用户可以向特定的人发送WhatsApp消息的应用程序。我尝试了一些我在互联网上找到的代码片段,但每当我尝试从实际设备发送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现在在对话活动中打开但是没有你在Intent设置的文本。 这是因为WhatsApp不支持这种共享。 您可以在WhatsApp常见问题解答中看到,与Intent唯一支持的共享是ACTION_SEND

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