gpt4 book ai didi

android - 在不使用 kotlin 传递消息的情况下将 WhatsApp 聊天窗口打开到特定号码

转载 作者:行者123 更新时间:2023-11-29 22:52:39 25 4
gpt4 key购买 nike

我需要将 WhatsApp 聊天窗口打开到特定号码而不传递消息

我试图删除 sendIntent.putExtra(Intent.EXTRA_TEXT, userText) 但没有帮助,当我从应用程序发送时它说

"Can't send empty message"

完整代码如下

    val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.action = Intent.ACTION_SEND
sendIntent.setPackage("com.whatsapp")
sendIntent.type = "text/plain"
sendIntent.putExtra("jid", number + "@s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, userText)
startActivity(sendIntent)

最佳答案

您可以使用 WhatsApp uri 打开特定的 whatsapp 聊天,而无需使用任何此类消息。这里的 num 可以是任何有效的数字,包括国家代码,例如 911234567890

private fun openWhatsApp(num: String) {
val isAppInstalled = appInstalledOrNot("com.whatsapp")
if (isAppInstalled) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=$num"))
startActivity(intent)
} else {
// WhatsApp not installed show toast or dialog
}
}

要检查是否安装了 WhatsApp,您可以使用此方法

 private fun appInstalledOrNot(uri: String): Boolean {
val pm = requireActivity().packageManager
return try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}

关于android - 在不使用 kotlin 传递消息的情况下将 WhatsApp 聊天窗口打开到特定号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57735153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com