在 Android "Can't send empty message" 上与 WhatsApp 分享文字

Share text with WhatsApp on Android "Can't send empty message"

当我尝试使用意图机制共享文本并选择 WhatsApp 时,它显示:

Can't send empty message

我在此处阅读了有关 Android 集成的官方文档:https://faq.whatsapp.com/en/android/28000012

我的代码:

public void shareText(String label, CharSequence title, CharSequence body) {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, title.toString());
        intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));

        final Intent chooser = Intent.createChooser(intent, label);
        chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (chooser.resolveActivity(mContext.getPackageManager()) != null) {
            mContext.startActivity(chooser);
        }
 }

我是不是做错了什么?还是 WhatsApp Messenger 的错误?

P.S。在我的例子中,参数 titlebody 不为空。

在这里您可以将数据从您的应用程序发送到 Whatsapp 和任何其他类似 messenger

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT,   " Your text ");
startActivity(Intent.createChooser(share,  " Your text "));

你所做的是,

intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));

while TextUtils.concat(title, body) returns CharSequence 可能是 whatsapp 不支持。

您必须将值作为字符串传递给您两个解决方案。

  • 可以通过toString()将整个转成String

intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body).toString());

  • 在将其传递给 intent 之前将其转换为字符串。

String someValue = TextUtils.concat(title, body).toString();

并将其添加为

intent.putExtra(Intent.EXTRA_TEXT, someValue);

这里sendEmtpyMassages是Button 复制这个方法就可以了

sendEmtpyMassages.setOnClickListener {
        val context: Context = applicationContext
        val sendIntent = Intent("android.intent.action.MAIN")
        sendIntent.action = Intent.ACTION_VIEW
        sendIntent.setPackage("com.whatsapp")
        val url = "https://api.whatsapp.com/send?phone=" + "&text=" + " "
        sendIntent.data = Uri.parse(url)
        if (sendIntent.resolveActivity(context.packageManager) != null) {
            startActivity(sendIntent)
        }
    }