Android:如何使用WhatsApp,WeChat以编程方式发送消息?

如何使用WhatsApp微信android应用程序中使用消息?

其实要求是使用WhatsApp和微信(免费短信)发送短信。

Solutions Collecting From Web of "Android:如何使用WhatsApp,WeChat以编程方式发送消息?"

我得到了解决scheme..在这里,我张贴的答案,以便它可以帮助其他人可能有同样的疑问..

通过任何应用程序共享…

public void sendAppMsg(View view) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); String text = " message you want to share.."; // change with required application package intent.setPackage("PACKAGE NAME OF THE APPLICATION"); if (intent != null) { intent.putExtra(Intent.EXTRA_TEXT, text);// startActivity(Intent.createChooser(intent, text)); } else { Toast.makeText(this, "App not found", Toast.LENGTH_SHORT) .show(); } } 

注意:根据您的要求更改*应用程序的包装名称

例如:USE

 //Whatsapp intent.setPackage("com.whatsapp");` //Linkedin intent.setPackage("com.linkedin.android"); //Twitter intent.setPackage("com.twitter.android"); //Facebook intent.setPackage("com.facebook.katana"); //GooglePlus intent.setPackage("com.google.android.apps.plus"); 

这应该有助于使用whatsapp发送消息:

 public void sendWhatsAppMsg(View view) { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "testing message"; waIntent.setPackage("com.whatsapp"); if (waIntent != null) { waIntent.putExtra(Intent.EXTRA_TEXT, text);// startActivity(Intent.createChooser(waIntent, text)); } else { Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT) .show(); } } 

发送直接消息给任何whatsapp用户使用以下代码:

 private void sendMessageToWhatsAppContact(String number) { PackageManager packageManager = context.getPackageManager(); Intent i = new Intent(Intent.ACTION_VIEW); try { String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.encode(CommonStrings.SHARING_APP_MSG, "UTF-8"); i.setPackage("com.whatsapp"); i.setData(Uri.parse(url)); if (i.resolveActivity(packageManager) != null) { context.startActivity(i); } } catch (Exception e) { e.printStackTrace(); } }