简体   繁体   English

没有应用可以执行此操作(Android)

[英]No Apps can perform this action android

I'm trying to make this program that when the user clicks a button, a message is sent to a whatsapp number 我正在尝试使该程序在用户单击按钮时将一条消息发送到whatsapp号

Here is the code in the onClick method 这是onClick方法中的代码

            Uri uri = Uri.parse("smsto:" + "xxxxxxxxxx"); //xxx.. is the mobile number
            Intent i = new Intent(Intent.ACTION_SENDTO, uri);
            i.putExtra(Intent.EXTRA_TEXT, "Check");
            i.setType("text/plain");
            i.setPackage("com.whatsapp");
            startActivity(Intent.createChooser(i, ""));

It shows that there are no apps that can perform this action. 它显示没有应用程序可以执行此操作。 Why? 为什么?

I removed 我删除了

            i.setType("text/plain");

And it works. 而且有效。 But the text "Check" is not sent. 但是不会发送文本“检查”。 How to do that if this is not the way. 如果不是这样,该怎么做。

Your format is slightly different from the example given on the WhatsApp FAQ, so I would modify to match. 您的格式与WhatsApp常见问题解答上给出的示例略有不同,因此我将进行修改以匹配。

From this page : 从此页面

Like most social apps on Android, WhatsApp listens to intents to share media and text. 与Android上的大多数社交应用程序一样,WhatsApp会听取共享媒体和文本的意图。 Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker: 例如,只需创建一个共享文本的意图,系统选择器就会显示WhatsApp:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

However, if you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent: 但是,如果您希望直接共享给WhatsApp并绕过系统选择器,则可以通过使用setPackage来实现:

sendIntent.setPackage("com.whatsapp");

This would simply be set right before you call startActivity(sendIntent); 只需在调用startActivity(sendIntent);之前设置即可。

Try this code to send ON whatsapp 尝试此代码以在whatsapp上发送

public void SendWhatsapp(View view) {

PackageManager pm=getPackageManager();
try {

    Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.setType("text/plain");
    String text = "YOUR TEXT HERE";

    PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
    //Check if package exists or not. If not then code 
    //in catch block will be called
    waIntent.setPackage("com.whatsapp");

    waIntent.putExtra(Intent.EXTRA_TEXT, text);
    startActivity(Intent.createChooser(waIntent, "Share with"));

   } catch (NameNotFoundException e) {
    Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
            .show();
  }  

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM