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();
}
}
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, ""));
}
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);
}
}
0
无论是否安装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;
}
0
在 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);
这将与一个人打开 direct 聊天,如果未安装whatsup这将throw exception,如果电话号码不知道whatsup他们将提供通过短信或简单的短信发送邀请
//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();
}
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);
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();
}
}
4
获取要发送消息的联系人号码并为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);
16 回答
检查此代码,
UPDATE 请参阅https://faq.whatsapp.com/en/android/26000030/?category=5245251
Original answer 这是解决方案
另见http://www.whatsapp.com/faq/en/android/28000012
使用此代码,您可以使用给定的号码打开whatsapp聊天 .
我找到了以下解决方案,首先你需要whatsapp id:
与此处和其他论坛中的其他一些主题的报告相匹配,我发现的登录名是某种形式:没有0的国际区号或者没有第一个0的开头电话号码@ s.whatsapp.net
例如,如果您居住在荷兰并且电话号码为0612325032,那么它将是31612325023@s.whatsapp.net - > 31,荷兰没有0,或者电话号码没有0 .
在 Marshmallow S5 上测试过它有效!
这将与一个人打开 direct 聊天,如果未安装whatsup这将throw exception,如果电话号码不知道whatsup他们将提供通过短信或简单的短信发送邀请
试试这个,这段代码通过Intent.ACTION_VIEW启动WhatsApp,不要忘记使用国家代码到电话号码 .
这对我有用:
以下代码由Google Now App使用,不适用于任何其他应用程序 .
我写这篇文章是因为它让我很生气,WhatsApp不允许任何其他开发者直接发送消息,除了谷歌 .
我希望其他自由开发人员知道,这种合作正在进行,而谷歌一直在谈论“对任何人开放”,WhatsApp表示他们不想向开发者提供任何访问权限 .
最近WhatsApp特别为Google Now添加了一个Intent,它应如下所示:
我还可以发现“search_action_token”是一个包含IBinder-Object的PendingIntent,如果它是由Google Now创建的,则会将其发送回Google App并进行检查 .
否则WhatsApp将不接受该消息 .
目前,the only official API您可以提出GET请求:
无论如何,有一个secret API program already being ran by WhatsApp
这是漫长而乖乖的工作 . 享受你的代码:)
我现在真的很晚,但我相信现在我们有更短更好的解决方案来通过WhatsApp发送消息 .
您可以使用以下命令调用系统选择器,然后选择您将使用哪个应用程序来共享您想要的任何内容 .
如果你真的需要通过WhatsApp发送所有你需要做的是以下(你将跳过系统选择器)
如果您需要更多信息,请在此处找到:WhatsApp FAQ
这对我有用:
获取要发送消息的联系人号码并为whatsapp创建uri,此处c是返回所选联系人的Cursor .
正如documentation所说,您可以使用以下URL:
https://wa.me/15551234567
最后一段是E164 Format中的数字
可以在c中使用以下API,如my article所示 .
您需要定义几个常量:
然后连接到API的 endpoints .
然后发送 Headers 和正文并等待需要“OK”的结果 .
第1步 - 打开HTTP请求:
第2步 - 发送 Headers :
第3步 - 发送消息:
现在只需查看结果: