whatsapp
是否可以直接从另一个应用程序向特定联系人发送消息?我知道联系人ID。我不想whatsapp
通过 Intent 打开。我只想像普通短信一样直接发送消息。
我已经尝试过在 stackoverflow 上发布的其他解决方案,但它们对我不起作用。
whatsapp
是否可以直接从另一个应用程序向特定联系人发送消息?我知道联系人ID。我不想whatsapp
通过 Intent 打开。我只想像普通短信一样直接发送消息。
我已经尝试过在 stackoverflow 上发布的其他解决方案,但它们对我不起作用。
请让我知道这对你有没有用,
Uri mUri = Uri.parse("smsto:+9876543210");
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(mIntent);
它不会立即发送消息,但可以让您在 whatsapp 中打开相同的屏幕:
private void openWhatsApp() {
String smsNumber = "91XXXXXXXX20";
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
} else {
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
Toast.makeText(this, "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
请试试这个。它对我来说工作得很好。
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
sendIntent.putExtra("jid", "9194******22" + "@s.whatsapp.net");// here 91 is country code
sendIntent.putExtra(Intent.EXTRA_TEXT, "Demo test message");
startActivity(sendIntent);
这将是工作,但你需要手机号码。国家代码,例如印度 91。例如。91758XXXXXX2
String url = "https://api.whatsapp.com/send?phone=" + 91758XXXXXX2 + "&text=" + URLEncoder.encode("good morning", "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
startActivity(i);
}
请试试这个,
public void onClickWhatsApp(View view) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
我希望这段代码可以帮助你
String text = "This is a test";// Replace with your message.
String toNumber = "xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country code
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+toNumber +"&text="+text));
startActivity(intent);