我正在尝试向特定号码发送 Whatsapp 消息,而不必保存该联系人。
这是我的代码
try {
Uri uri = Uri.parse("smsto: " + smsNumber);
//Timber.e("smsNumber %s", uri.toString());
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
} catch (Exception e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
以及我从中得到的信息
在这里试试这个。这对我有帮助
private void openWhatsApp() {
String smsNumber = "+26377*********";
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;
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_IMAGE_CODE) {
if (data != null) {
val image = data.data
val number = "9163********"
val whatsappIntent = Intent(Intent.ACTION_SEND)
whatsappIntent.setPackage("com.whatsapp")
whatsappIntent.setType("text/plain")
whatsappIntent.putExtra(
Intent.EXTRA_TEXT,
"I am sending the image for testing"
)
whatsappIntent.putExtra(Intent.EXTRA_STREAM, image)
whatsappIntent.setType("image/*")
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
whatsappIntent.putExtra(
"jid",
PhoneNumberUtils.stripSeparators(number) + "@s.whatsapp.net"
) //phone number without "+" prefix
startActivity(whatsappIntent)
}
}
}
}
在清单中:(应用程序标签下方)(适用于 Android 11 支持)
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.whatsapp.w4b" />
</queries>
它会对你有帮助,对我来说效果很好
使用此功能后,您可以从存储中选择文件并将其发送到 Whatsapp 上未保存的号码以及一些文本
嗨,你可以使用这个代码:
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);
它会对你有帮助
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("**91**"+smsnumber) + "@s.whatsapp.net");//phone number without "+" prefix
context.startActivity(sendIntent);
在尝试了许多解决方案后,这个解决方案适用于所有 Android 版本
1.在AndroidManifest中添加此内容
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.whatsapp.w4b" />
</queries>
使用此代码的手机号码,带国家代码,不带“+”
PackageManager packageManager = getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
String contact = "91"+"9713*****6" //"Contry code"+"whatsApp number"
try {
String url = "https://api.whatsapp.com/send?phone=" + contact + "&text=" + URLEncoder.encode("test to send", "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
startActivity(i);
}
} catch (Exception e) {
e.printStackTrace();
}