Android:如何使用WhatsApp、微信以编程方式发送消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24774595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:44:21  来源:igfitidea点击:

Android: How to send message programmatically by using WhatsApp, WeChat?

androidsmssocial-networking

提问by Subhalaxmi

How to use messaging in android application by using WhatsAppand WeChat?

如何通过使用WhatsApp微信在 android 应用程序中使用消息传递?

Actually requirement is to send sms using WhatsApp and WeChat (Free sms).

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

回答by Subhalaxmi

I got the Solution.. Here I am posting the answer so that it may help other people who may have same doubt..

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

For Share through any application...

通过任何应用程序共享...

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

Note : change *PACKAGE NAME OF THE APPLICATIONas per your requirement like

注意:根据您的要求更改应用程序的*PACKAGE NAME

Example : 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");

回答by MysticMagic?

This should help to send message using whatsapp:

这应该有助于使用 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();
    }

}

回答by NehaK

To send direct message to any whatsapp user use following code :

要向任何 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();
    }
}

回答by MohamedHarmoush

you can try one of these two solutions they worked for me.

您可以尝试他们为我工作的这两种解决方案之一。

reference: https://faq.whatsapp.com/en/android/26000030/?category=5245251

参考:https: //faq.whatsapp.com/en/android/26000030/?category=5245251

private void openWhatsApp(String countryCode, String mobile) {
    String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
    Uri uri = Uri.parse("smsto:" + customerPhoneNumber);
    Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
    sendIntent.setPackage("com.whatsapp");

    if(sendIntent.resolveActivity(getPackageManager()) == null){
        showDialogMessage("you should install whatsapp.");
        return;
    }

    startActivity(sendIntent);
}

or

private void openWhatsApp(String countryCode, String mobile) {
    String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setPackage("com.whatsapp");
// you can remove this part ("&text=" + "your message")
    String url = "https://api.whatsapp.com/send?phone=" + customerPhoneNumber + "&text=" + "your message"; 
    sendIntent.setData(Uri.parse(url));

    if(sendIntent.resolveActivity(getPackageManager()) == null){
        showDialogMessage("you should install whatsapp.");
        return;
    }

    startActivity(sendIntent);
}

回答by gouri panda

This is the simplest example that I get

这是我得到的最简单的例子

/**
 * sends message to the specific application
 * @param text The message that user wants to send
 * @param view is the activity view of the application
 */
public void sendMessage(String text, View view){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setType("text/plain");
    intent.setPackage("com.example.appname");//For whatsapp you can use com.whatsapp
    if (intent !=null){
        intent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(intent);
    }else{
        Snackbar.make(this, "Application not found.", Snackbar.LENGTH_SHORT).show();
    }
}