由于我发现了一些较旧的帖子,告诉whatsapp不支持这一点,我想知道是否有什么变化,以及是否有办法用我正在发送的号码打开一个whatsapp'聊天'意图是什么?
答案 0 :(得分:159)
<强>更新强> 请参阅https://faq.whatsapp.com/en/android/26000030/?category=5245251
WhatsApp的点击聊天功能可让您开始聊天 没有将手机号码保存在手机中的人 地址簿。只要你知道这个人的电话号码,你就可以 创建一个链接,允许您开始与他们聊天。
使用:https://api.whatsapp.com/send?phone=15551234567
请勿使用:https://api.whatsapp.com/send?phone=+001-(555)1234567
示例:https://api.whatsapp.com/send?phone=15551234567&text=I'%20感兴趣%20in%20your%20car%20for%20sale
原始回答 这是解决方案
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
答案 1 :(得分:56)
使用此代码,您可以使用给定的号码打开whatsapp聊天。
void openWhatsappContact(String number) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
}
答案 2 :(得分:25)
我找到了以下解决方案,首先你需要whatsapp id:
与此处和其他论坛中的其他线程的报告匹配我找到的登录名是某种形式: 国际区号没有0或+开头+电话号码没有第一个0 + @ s.whatsapp.net
例如,如果您居住在荷兰并且电话号码为0612325032,则为31612325023@s.whatsapp.net - &gt;没有0或+的荷兰+31,没有0的电话号码。
public void sendWhatsAppMessageTo(String whatsappid) {
Cursor c = getSherlockActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
new String[] { whatsappid }, null);
c.moveToFirst();
Intent whatsapp = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
c.close();
if (whatsapp != null) {
startActivity(whatsapp);
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
//download for example after dialog
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
}
}
答案 3 :(得分:14)
无论是否安装Whatsapp,这都应该有效。
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Uri uri = Uri.parse("smsto:" + "98*********7")
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hai Good Morning");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
} else {
Toast.makeText(this, "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
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;
}
答案 4 :(得分:9)
在 Marshmallow S5 上进行测试,确实有效!
Uri uri = Uri.parse("smsto:" + "phone number with country code");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
这将打开与某人的直接聊天,如果没有安装whatsup这将throw exception,如果电话号码不知道whatsup他们将提供通过短信或简单的短信发送邀请消息
答案 5 :(得分:5)
试试这个,此代码通过Intent.ACTION_VIEW启动WhatsApp,不要忘记使用国家/地区代码到电话号码。
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(
"https://api.whatsapp.com/send?phone=+628119xxx&text=I'm%20interested%20in%20your%20car%20for%20sale"
)));
答案 6 :(得分:3)
目前,the only official API您可以向GET
提出以下请求:
https://api.whatsapp.com/send?phone=919773207706&text=Hello
答案 7 :(得分:2)
以下代码由Google即时应用程序使用,不适用于任何其他应用程序。
我写这篇文章是因为它让我很生气,WhatsApp不允许任何其他开发者直接发送消息,除了谷歌。
我希望其他自由职业者开发人员知道,这种合作正在进行,而谷歌一直在谈论&#34;对所有人开放#34; WhatsApp表示他们不想向开发者提供任何访问权限。
最近,WhatsApp特别为Google Now添加了一个Intent,它应如下所示:
Intent intent = new Intent("com.google.android.voicesearch.SEND_MESSAGE_TO_CONTACTS");
intent.setPackage("com.whatsapp");
intent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.VoiceMessagingActivity"));
intent.putExtra("com.google.android.voicesearch.extra.RECIPIENT_CONTACT_CHAT_ID", number);
intent.putExtra("android.intent.extra.TEXT", text);
intent.putExtra("search_action_token", ?????);
我也可以找到&#34; search_action_token&#34;是PendingIntent 它包含一个IBinder-Object,如果它是由Google Now创建的,则会被发送回Google App并进行检查。
否则WhatsApp将不接受该消息。
答案 8 :(得分:2)
这对我有用:
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,
ri.activityInfo.name));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, element);
} catch (NameNotFoundException e) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
答案 9 :(得分:2)
使用whatsapp的直接URL
String url = "https://api.whatsapp.com/send?phone="+number;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
答案 10 :(得分:1)
这是通过 Whatsapp
发送消息的最新方式,即使接收者的电话号码不在您的 Whatsapp
聊天或电话的 Contacts
列表中。
private fun openWhatsApp(number: String) {
try {
packageManager.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES)
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://wa.me/$number?text=I'm%20interested%20in%20your%20car%20for%20sale")
)
intent.setPackage("com.whatsapp")
startActivity(intent)
} catch (e: PackageManager.NameNotFoundException) {
Toast.makeText(
this,
"Whatsapp app not installed in your phone",
Toast.LENGTH_SHORT
).show()
e.printStackTrace()
}
}
intent.setPackage("com.whatsapp")
将帮助您避免打开方式 选择器并直接打开 Whatsapp
。
重要提示:如果您以 catch
语句结尾,即使安装了 Whatsapp
。请将 queries
添加到 manifest.xml
如下:
<queries>
<package android:name="com.whatsapp" />
</queries>
有关详细信息,请参阅此 answer。
答案 11 :(得分:1)
这是漫长而艰苦的工作。 享受你的代码:)
//method used to show IMs
private void show_custom_chooser(String value) {
List<ResolveInfo> list = null;
final Intent email = new Intent(Intent.ACTION_SEND);
email.setData(Uri.parse("sms:"));
email.putExtra(Intent.EXTRA_TEXT, "" + value);
email.setType("text/plain"); // vnd.android-dir/mms-sms
WindowManager.LayoutParams WMLP = dialogCustomChooser.getWindow()
.getAttributes();
WMLP.gravity = Gravity.CENTER;
dialogCustomChooser.getWindow().setAttributes(WMLP);
dialogCustomChooser.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialogCustomChooser.setCanceledOnTouchOutside(true);
dialogCustomChooser.setContentView(R.layout.about_dialog);
dialogCustomChooser.setCancelable(true);
ListView lvOfIms = (ListView) dialogCustomChooser
.findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ResolveInfo> launchables = pm.queryIntentActivities(email, 0);
// ////////////new
list = new ArrayList<ResolveInfo>();
for (int i = 0; i < launchables.size(); i++) {
String string = launchables.get(i).toString();
Log.d("heh", string);
//check only messangers
if (string.contains("whatsapp")) {
list.add(launchables.get(i));
}
}
Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm));
int size = launchables.size();
adapter = new AppAdapter(pm, list, MainActivity.this);
lvOfIms.setAdapter(adapter);
lvOfIms.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
ResolveInfo launchable = adapter.getItem(position);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(
activity.applicationInfo.packageName, activity.name);
email.addCategory(Intent.CATEGORY_LAUNCHER);
email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
email.setComponent(name);
startActivity(email);
dialogCustomChooser.dismiss();
}
});
dialogCustomChooser.show();
}
答案 12 :(得分:1)
要检查设备中是否安装了WhatsApp并在WhatsApp中启动“单击聊天”:
科特琳:
+-------------+---------+----------+---------+
| Row Index | Col1 | Col2 | Col3 |
+-------------+---------+----------+---------+
| 1 | pasta | pastas | YES |
| 2 | sauces | orange | NO |
| 3 | rosin | robert | NO |
+-------------+---------+----------+---------+
Java:
try {
// Check if whatsapp is installed
context?.packageManager?.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA)
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/$internationalPhoneNumber"))
startActivity(intent)
} catch (e: NameNotFoundException) {
Toast.makeText(context, "WhatsApp not Installed", Toast.LENGTH_SHORT).show()
}
如果未安装WhatsApp,则 try {
// Check if whatsapp is installed
getPackageManager().getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
Intent intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/" + internationalPhoneNumber));
startActivity(intent);
} catch (NameNotFoundException e) {
Toast.makeText(context, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
会抛出getPackageInfo()
。
NameNotFoundException
变量用于访问电话号码。
参考:
答案 13 :(得分:1)
您将要使用以下格式的URL ...
https://api.whatsapp.com/send?text=text
然后,您可以让它发送您想要的任何文本。您还可以选择指定电话号码...
https://api.whatsapp.com/send?text=text&phone=1234
您 不能做 的内容使用以下内容:
https://wa.me/send?text=text
你会得到...
但是,如果同时提供电话号码和文本,则我们找不到您要查找的页面
wa.me
将起作用。但是,在大多数情况下,如果您尝试建立共享链接,则实际上并不想指明电话号码,因为您希望用户选择某人。在这种情况下,如果您不提供该号码并使用wa.me
作为URL,则所有共享链接都将失败。请使用app.whatsapp.com
。
答案 14 :(得分:1)
正如documentation所说,您可以使用如下网址:
https://wa.me/15551234567
最后一段是E164 Format中的数字
Uri uri = Uri.parse("https://wa.me/15551234567");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
答案 15 :(得分:1)
我真的很晚了,但我相信现在我们有更短更好的解决方案来通过WhatsApp发送消息。
您可以使用以下方法调用系统选择器,然后选择您将用于分享您想要的任何应用程序。
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);
如果你真的需要通过WhatsApp发送所有你需要做的是以下(你将跳过系统选择器)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
// Put this line here
sendIntent.setPackage("com.whatsapp");
//
startActivity(sendIntent);
如果您需要更多信息,可以在此处找到:WhatsApp FAQ
答案 16 :(得分:0)
expo start -c
答案 17 :(得分:0)
这对我有用:
Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + "<number>" + "&text=" + "Hello WhatsApp!!");
Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(sendIntent);
答案 18 :(得分:0)
private fun sendWhatsappMessage(phoneNumber:String, text:String) {
val url = if (Intent().setPackage("com.whatsapp").resolveActivity(packageManager) != null) {
"whatsapp://send?text=Hello&phone=$phoneNumber"
} else {
"https://api.whatsapp.com/send?phone=$phoneNumber&text=$text"
}
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(browserIntent)
}
这是实现此目的的简便得多的方法。此代码检查设备上是否安装了whatsapp。如果已安装,它将绕过系统选择器,并转到whatsapp上的联系人,并预字段聊天中的文本。如果未安装,它将在您的Web浏览器上打开whatsapp链接。
答案 19 :(得分:0)
使用预填充的消息创建自己的链接,该消息将 自动出现在聊天的文本字段中,使用 https://wa.me/whatsappphonenumber/?text=urlencodedtext其中 whatsappphonenumber是国际格式的完整电话号码, URL编码的文本是URL编码的预填充消息。
例如:https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale
val phoneNumber = "13492838472"
val text = "Hey, you know... I love StackOverflow :)"
val uri = Uri.parse("https://wa.me/$phoneNumber/?text=$text")
val sendIntent = Intent(Intent.ACTION_VIEW, uri)
startActivity(sendIntent)
答案 20 :(得分:0)
获取您要发送消息的联系号码并为whatsapp创建uri,此处c是返回所选联系人的Cursor。
Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp"); // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);
答案 21 :(得分:0)
检查此代码,
public void share(String subject,String text) {
final Intent intent = new Intent(Intent.ACTION_SEND);
String score=1000;
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, score);
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(intent, getString(R.string.share)));
}
答案 22 :(得分:0)
这对我有用:
public static void shareWhatsApp(Activity appActivity, String texto) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, texto);
PackageManager pm = appActivity.getApplicationContext().getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(sendIntent, 0);
boolean temWhatsApp = false;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.startsWith("com.whatsapp")) {
final ComponentName name = new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setComponent(name);
temWhatsApp = true;
break;
}
}
if(temWhatsApp) {
//abre whatsapp
appActivity.startActivity(sendIntent);
} else {
//alerta - você deve ter o whatsapp instalado
Toast.makeText(appActivity, appActivity.getString(R.string.share_whatsapp), Toast.LENGTH_SHORT).show();
}
}
答案 23 :(得分:0)
这个最终在 Kotlin 中对我有用:
private fun navigateToWhatsApp() {
try {
val url = "https://api.whatsapp.com/send?phone=+91${getString(R.string.contact)}"
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).setPackage("com.whatsapp"))
} catch (e: Exception) {
showToast("Whatsapp app not installed in your device")
}
}
答案 24 :(得分:-1)
如my article所示,以下API可以在c ++中使用。
您需要定义几个常量:
//
#define GroupAdmin <YOUR GROUP ADMIN MOBILE PHONE>
#define GroupName <YOUR GROUP NAME>
#define CLIENT_ID <YOUR CLIENT ID>
#define CLIENT_SECRET <YOUR CLIENT SECRET>
#define GROUP_API_SERVER L"api.whatsmate.net"
#define GROUP_API_PATH L"/v3/whatsapp/group/text/message/12"
#define IMAGE_SINGLE_API_URL L"http://api.whatsmate.net/v3/whatsapp/group/image/message/12"
//
然后,您连接到API的端点。
hOpenHandle = InternetOpen(_T("HTTP"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hOpenHandle == NULL)
{
return false;
}
hConnectHandle = InternetConnect(hOpenHandle,
GROUP_API_SERVER,
INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP,
0, 1);
if (hConnectHandle == NULL)
{
InternetCloseHandle(hOpenHandle);
return false;
}
然后发送标头和正文,并等待结果必须为“ OK”。
第1步-打开HTTP请求:
const wchar_t *AcceptTypes[] = { _T("application/json"),NULL };
HINTERNET hRequest = HttpOpenRequest(hConnectHandle, _T("POST"), GROUP_API_PATH, NULL, NULL, AcceptTypes, 0, 0);
if (hRequest == NULL)
{
InternetCloseHandle(hConnectHandle);
InternetCloseHandle(hOpenHandle);
return false;
}
第2步-发送标头:
std::wstring HeaderData;
HeaderData += _T("X-WM-CLIENT-ID: ");
HeaderData += _T(CLIENT_ID);
HeaderData += _T("\r\nX-WM-CLIENT-SECRET: ");
HeaderData += _T(CLIENT_SECRET);
HeaderData += _T("\r\n");
HttpAddRequestHeaders(hRequest, HeaderData.c_str(), HeaderData.size(), NULL);
第3步-发送消息:
std::wstring WJsonData;
WJsonData += _T("{");
WJsonData += _T("\"group_admin\":\"");
WJsonData += groupAdmin;
WJsonData += _T("\",");
WJsonData += _T("\"group_name\":\"");
WJsonData += groupName;
WJsonData += _T("\",");
WJsonData += _T("\"message\":\"");
WJsonData += message;
WJsonData += _T("\"");
WJsonData += _T("}");
const std::string JsonData(WJsonData.begin(), WJsonData.end());
bResults = HttpSendRequest(hRequest, NULL, 0, (LPVOID)(JsonData.c_str()), JsonData.size());
现在只需检查结果即可:
TCHAR StatusText[BUFFER_LENGTH] = { 0 };
DWORD StatusTextLen = BUFFER_LENGTH;
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_TEXT, &StatusText, &StatusTextLen, NULL);
bResults = (StatusTextLen && wcscmp(StatusText, L"OK")==FALSE);