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

Sub*_*xmi 22 sms android social-networking

如何使用WhatsAppWeChat在Android应用程序中使用消息?

实际上要求是使用WhatsApp和WeChat(免费短信)发送短信.

Sub*_*xmi 29

我得到了解决方案..在这里,我发布了答案,以便它可以帮助其他可能有同样怀疑的人.

通过任何申请分享......

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

示例: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");
Run Code Online (Sandbox Code Playgroud)

  • @SubhalaxmiNayak我尝试使用intent.setPackage("com.tencent.mm"); 它工作但它打开对话框有两个选项"微信"和"微信收藏夹".是否可以直接打开微信 (2认同)
  • 这只会打开whatsApp,如果你想直接发送给特定的联系人,而不点击联系人. (2认同)

Mys*_*icϡ 15

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

public void sendWhatsAppMsg() {

    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();
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 无论如何指定收件人? (3认同)
  • @CostaMirkin没什么好主意的.但请查看[this](https://github.com/cblunt/android-emoticon-edittext-spike)和[this](http://androidcodeexamples.blogspot.in/2011/08/how-to-add-smileyemojis -in-edittext.html) (2认同)

Neh*_*haK 12

要向任何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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我在不同的帖子中发表了相同评论的类似答案,他们回答说,由于whatsapp的安全问题,他们不允许这样做。我认为这是合理的。想象一下某个未知的应用程序在使用您的服务发送消息而不受用户干扰。但是我敢肯定,如果我们可以在屏幕上模拟一个触摸事件,那仍然可能。但是,可以使用通常的消息应用程序直接发送消息,而无需等待用户的批准。 (2认同)