如何使用意图以编程方式向特定的 whatsapp 联系人发送消息?
How to send message to particular whatsapp contact programmatically using intent?
我搜索了各种答案,但所有答案都已过时。
每当我尝试使用发送时,它只会打开联系人选择器。
你必须像这样在 Intent 中设置包名
intent.setPackage("com.whatsapp");
完整示例:
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
您只需在点击按钮时触发以下意图:
Uri mUri = Uri.parse("smsto:" + mobile1);
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat", true);
startActivity(Intent.createChooser(mIntent, ""));
如果号码在 whatsapp 上可用,则该特定用户聊天打开,您发送 message.If 号码在 whatsapp 上不可用,在这种情况下,警报对话框打开。
希望对您有所帮助 ;-)
这是解决方案
private void openWhatsApp(String number) {
String whatsAppMessage = "Hello!";
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);
}
调用上面的函数并传递号码,想要在Whatsapp messenger中打开聊天。
希望对您有用。 :)
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"@s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
更新:
前面提到的 hack 不能用来添加任何特定的消息,所以这是新方法。在此处以国际格式传递用户 mobile,不带任何括号、破折号或加号。示例:如果用户是印度人并且他的手机号码是 94xxxxxxxx ,那么国际格式将是 9194xxxxxxxx。不要错过在手机号码中附加国家代码作为前缀。
private fun sendMsg(mobile: String, msg: String){
try {
val packageManager = requireContext().packageManager
val i = Intent(Intent.ACTION_VIEW)
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
i.setPackage("com.whatsapp")
i.data = Uri.parse(url)
if (i.resolveActivity(packageManager) != null) {
requireContext().startActivity(i)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Note: This approach works only with contacts added in user's Whatsapp
account.
我搜索了各种答案,但所有答案都已过时。 每当我尝试使用发送时,它只会打开联系人选择器。
你必须像这样在 Intent 中设置包名
intent.setPackage("com.whatsapp");
完整示例:
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
您只需在点击按钮时触发以下意图:
Uri mUri = Uri.parse("smsto:" + mobile1);
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat", true);
startActivity(Intent.createChooser(mIntent, ""));
如果号码在 whatsapp 上可用,则该特定用户聊天打开,您发送 message.If 号码在 whatsapp 上不可用,在这种情况下,警报对话框打开。
希望对您有所帮助 ;-)
这是解决方案
private void openWhatsApp(String number) {
String whatsAppMessage = "Hello!";
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);
}
调用上面的函数并传递号码,想要在Whatsapp messenger中打开聊天。
希望对您有用。 :)
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"@s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
更新:
前面提到的 hack 不能用来添加任何特定的消息,所以这是新方法。在此处以国际格式传递用户 mobile,不带任何括号、破折号或加号。示例:如果用户是印度人并且他的手机号码是 94xxxxxxxx ,那么国际格式将是 9194xxxxxxxx。不要错过在手机号码中附加国家代码作为前缀。
private fun sendMsg(mobile: String, msg: String){
try {
val packageManager = requireContext().packageManager
val i = Intent(Intent.ACTION_VIEW)
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
i.setPackage("com.whatsapp")
i.data = Uri.parse(url)
if (i.resolveActivity(packageManager) != null) {
requireContext().startActivity(i)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Note: This approach works only with contacts added in user's Whatsapp account.