我正在开发一个Android应用程序,我需要向WhatsApp的特定联系人发送消息。 我试过这段代码:
Uri mUri = Uri.parse("smsto:+999999999");
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没有收到参数“sms_body”,但选择了联系人。
答案 0 :(得分:17)
有一种方法。确保您提供的联系人必须作为意向字符串传递,而不带前缀“+”。国家/地区代码应作为电话号码的前缀附加。
例如:'+ 918547264285'应作为'918547264285'传递。这里的'91'开头是国家代码。
注意:将“YOUR_PHONE_NUMBER”替换为您要向其发送消息的联系人。
以下是摘录:
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");
startActivity(sendIntent);
答案 1 :(得分:5)
这种新方法,通过Android中的whatsapp向特定联系人发送消息。有关详细信息,请查看here
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + path;
sendIntent.setData(Uri.parse(url));
activity.startActivity(sendIntent);here
答案 2 :(得分:2)
我找到了正确的方法,在您阅读本文之后,这很简单:http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/
电话和留言都是字符串。
源代码:
sbt run foo bar
享受!
答案 3 :(得分:1)
很棒的黑客Rishabh,非常感谢,自从过去3年以来我一直在寻找这个解决方案。
根据Rishabh Maurya上面的回答,我已经实现了这个代码,它对WhatsApp上的文本和图像共享都很好。我已经在我的Android应用程序中发布了这个,所以如果你想看到它,请试试我的应用Bill Book
请注意,在这两种情况下,它都会打开一个whatsapp会话(如果toNumber存在于用户whatsapp联系人列表中),但用户必须单击“发送”按钮才能完成操作。这意味着它有助于跳过联系人选择步骤。
用于短信
String toNumber = "+91 98765 43210"; // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
startActivity(sendIntent);
用于分享图像
String toNumber = "+91 98765 43210"; // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("image/png");
context.startActivity(sendIntent);
享受WhatsApping!
答案 4 :(得分:1)
您可以使用以下代码:
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);
答案 5 :(得分:1)
这是通过Whatsapp将邮件发送到特定号码或未保存号码的最佳方法
private void openWhatsApp() {
String smsNumber = "252634651588";
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(getContext(), "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = Objects.requireNonNull(getContext()).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;
}
答案 6 :(得分:0)
这对我有用。
参数' body' whatsapp app不会变红,使用' Intent.EXTRA_TEXT'代替。
设置' phoneNumber'您指定要在whatsapp中打开的联系人。
[
{
"id":"Item 1",
"name": "Item 1 Name",
"image": "./item1.jpg",
"number": "Number 1"
},
{
"id":"Item 2",
"name": "Item 2 Name",
"image": "./item2.jpg",
"number": "Number 2"
},
...,
...
]
答案 7 :(得分:0)
Uri mUri = Uri.parse("smsto:+90000900000");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("chat",true);
startActivity(Intent.createChooser(mIntent, "Share with"));
非常适合从我的Android应用程序
向WhatsApp上的特定联系人发送消息答案 8 :(得分:0)
我们可以分享/发送消息到什么应用程序。以下是在Whats-app
- 单个用户
醇>
private void shareToOneWhatsAppUser(String message) {
/**
* NOTE:
* Message is shared with only one user at a time. and to navigate back to main application user need to click back button
*/
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, message);
//Directly send to specific mobile number...
String smsNumber = "919900990099";//Number without with country code and without '+' prifix
whatsappIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
if (whatsappIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(MainActivity.this, "Whatsapp not installed.", Toast.LENGTH_SHORT).show();
return;
}
startActivity(whatsappIntent);
}
- 多个用户
醇>
private void shareToMultipleWhatsAppUser(String message) {
/**
* NOTE:
*
* If want to send same message to multiple users then have to select the user to whom you want to share the message & then click send.
* User navigate back to main Application once he/she select all desired persons and click send button.
* No need to click Back Button!
*/
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, message);
if (whatsappIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(MainActivity.this, "Whatsapp not installed.", Toast.LENGTH_SHORT).show();
return;
}
startActivity(whatsappIntent);
}
实现相同的另一种方式
private void shareDirecctToSingleWhatsAppUser(String message) {
/**
* NOTE:
* Message is shared with only one user at a time. and to navigate back to main application user need to click back button
*/
//Directly send to specific mobile number...
String smsNumber = "919900000000";//Intended user`s mobile number with country code & with out '+'
PackageManager packageManager = getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone="+ smsNumber +"&text=" + URLEncoder.encode("Test Message!", "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
}
答案 9 :(得分:0)
尝试此代码
Uri uri = Uri.parse("smsto:" + "+6281122xxx");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.default_message_wa));
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
您不能像这样将字符串直接放在putExtra上
i.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
更改代码并从这样的资源获取字符串
i.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.default_message_wa));
答案 10 :(得分:0)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
String url ="https://wa.me/your number";
sendIntent.setData(Uri.parse(url));
startActivity(sendIntent);
答案 11 :(得分:0)
这是我的方法:
首先,您检查该人是否甚至拥有WhatsApp帐户:
@RequiresPermission(permission.READ_CONTACTS)
public String getContactMimeTypeDataId(@NonNull Context context, String contactId, @NonNull String mimeType) {
if (TextUtils.isEmpty(mimeType) || !PermissionUtil.hasPermissions(context, Manifest.permission.READ_CONTACTS))
return null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID}, Data.MIMETYPE + "= ? AND "
+ ContactsContract.Data.CONTACT_ID + "= ?", new String[]{mimeType, contactId}, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(Data._ID));
cursor.close();
return result;
}
如果一切看起来不错,则可以像从网络上打开它一样>
final String contactMimeTypeDataId = getContactMimeTypeDataId(context, contactId, "vnd.android.cursor.item/vnd.com.whatsapp.profile");
if (contactMimeTypeDataId != null) {
final String whatsAppPhoneNumber = PhoneNumberHelper.normalizePhone(phoneNumber);
String url = "https://api.whatsapp.com/send?phone="+ whatsAppPhoneNumber ;
intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
.setPackage("com.whatsapp");
startActivity(intent);
}
您还可以在所有这些之前检查是否已安装WhatsApp:
final PackageManager packageManager = context.getPackageManager();
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo("com.whatsapp", 0);
if (applicationInfo == null)
return;
答案 12 :(得分:-2)
尝试使用Intent.EXTRA_TEXT
代替sms_body
作为额外密钥。根据WhatsApp的文档,这是你必须使用的。
来自their website的示例:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
他们的示例使用Intent.ACTION_SEND
代替Intent.ACTION_SENDTO
,所以我不确定WhatsApp是否支持通过意图系统直接发送给联系人。一些快速测试应该让你确定。