通过WhatsApp发送消息
因为我发现了一些较旧的帖子,告诉whatsapp不支持这个,我想知道是否有什么改变,如果有一种方法来打开一个whatsapp与我通过意图发送的号码“聊天”?
这是解决方案
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(); } }
有了这个代码,你可以打开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, "")); }
我找到了以下解决方案,首先你需要whatsapp id:
在这里和其他论坛上与其他论坛的报告相匹配,我发现的登录名是某种类型:国际区号,没有0或者在开始时+电话号码,没有第一个0 + @ s.whatsapp.net
例如,如果你住在荷兰,并且电话号码是0612325032,那么荷兰没有0或者+而电话号码是31612325023@s.whatsapp.net-> +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); } }
无论是否安装了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; }
测试棉花糖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没有安装这将抛出异常 ,如果电话号码不知道whatsup他们将提供通过短信或简单的短信发送邀请
这对我有用:
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."); }
这对我有用:
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(); } }
这是非常漫长的,但是勉强工作。 享受你的代码:)
//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(); }
以下代码由Google即时应用使用,不适用于任何其他应用程序。
我写这篇文章是因为它让我很生气,WhatsApp不允许任何其他开发者直接发送消息,除了Google。
我希望其他自由开发者知道,这种合作正在进行,而谷歌一直在谈论“对任何人开放”,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", ?????);
我还可以发现“search_action_token”是一个PendingIntent,它包含一个IBinder对象,如果它是由Google Now创建的,它将被发送回Google App并进行检查。
否则WhatsApp将不会接受该消息。
我在这里真的很晚,但我相信现在我们有更短更好的解决方案来通过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的常见问题
检查这个答案,并尝试它,这个答案启动WhatsApp通过Intent.ACTION_VIEW
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse( "https://api.whatsapp.com/send?phone=+628119xxx&text=I'm%20interested%20in%20your%20car%20for%20sale" )));
检查这个代码,
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))); }