在 Android 上与 WhatsApp 共享文本“无法发送空消息”

Ole*_*bul 4 android whatsapp

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

无法发送空消息

我在这里阅读了有关 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);
        }
 }
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?或者是WhatsApp Messenger的错误?

PS 参数title并且body在我的情况下不是空的。

san*_*eev 6

你所做的是,

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

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

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

  • 您可以通过 toString() 将整个转换为字符串

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

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

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

并将其添加到此处,

intent.putExtra(Intent.EXTRA_TEXT, someValue);
Run Code Online (Sandbox Code Playgroud)