我想仅使用whatsapp向特定联系人发送消息,而无需用户从其联系人列表中选择它。我使用以下代码来激发意图
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
但这会打开whatsapp联系人屏幕,用户必须从中选择联系人姓名。
答案 0 :(得分:2)
请尝试以下代码。
try {
String num= "+918888888888"; // contains spaces.
num= num.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra("jid", num+ "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
startActivity(sendIntent);
} catch (Exception e) {
app.showSnackBar(parentView, "Whatsapp have not been installed.");
}