首页 > 解决方案 > 通过单击我的应用程序(Kotlin)中的按钮打开与特定联系人的 WhatsApp 聊天

问题描述

如何通过单击我的应用程序中的按钮打开与特定联系人的 Whatsapp 聊天?

这就是我使用的代码。它会打开 WhatsApp 并让我搜索我想向其发送消息的联系人,但它不会打开带有我提供给它的特定联系人号码的 WhatsApp 聊天。

        whatsappButton.setOnClickListener{
            var con = itemView.context

            val textToShare = "*כח אדם*"
            val phoneNumber = blogPost.phone
            val sendIntent = Intent()

            sendIntent.action = Intent.ACTION_SEND
            sendIntent.type = "text/plain"
            sendIntent.putExtra("jid", phoneNumber+"@s.whatsapp.net")
            sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare)
            val extra = sendIntent.extras
            startActivity(con,sendIntent,extra)
        }

标签: androidkotlinandroid-intentwhatsappstart-activity

解决方案


请在此处检查此答案 使用以下代码:

Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp");           // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);

推荐阅读