Sending message through WhatsApp

前端 未结 23 2206
萌比男神i
萌比男神i 2020-11-22 09:42

Since I found some older posts, that tell that whatsapp doesn\'t support this, I was wondering if something had changed and if there is a way to open a whatsapp \'chat\' wit

相关标签:
23条回答
  • 2020-11-22 10:05

    Use direct URL of whatsapp

    String url = "https://api.whatsapp.com/send?phone="+number;
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    
    0 讨论(0)
  • 2020-11-22 10:07

    UPDATE Please refer to https://faq.whatsapp.com/en/android/26000030/?category=5245251

    WhatsApp's Click to Chat feature allows you to begin a chat with someone without having their phone number saved in your phone's address book. As long as you know this person’s phone number, you can create a link that will allow you to start a chat with them.

    Use: https://wa.me/15551234567

    Don't use: https://wa.me/+001-(555)1234567

    Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

    Original answer Here is the solution

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

    Also see http://www.whatsapp.com/faq/en/android/28000012

    0 讨论(0)
  • 2020-11-22 10:08

    Currently, the only official API that you may make a GET request to:

    https://api.whatsapp.com/send?phone=919773207706&text=Hello
    

    Anyways, there is a secret API program already being ran by WhatsApp

    0 讨论(0)
  • 2020-11-22 10:08

    This works to me:

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:10

    This works to me:

    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.");
    }
    
    0 讨论(0)
  • 2020-11-22 10:10

    From the documentation

    To create your own link with a pre-filled message that will automatically appear in the text field of a chat, use https://wa.me/whatsappphonenumber/?text=urlencodedtext where whatsappphonenumber is a full phone number in international format and URL-encodedtext is the URL-encoded pre-filled message.

    Example:https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

    Code example

    val phoneNumber = "13492838472"
        val text = "Hey, you know... I love StackOverflow :)"
        val uri = Uri.parse("https://wa.me/$phoneNumber/?text=$text")
        val sendIntent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(sendIntent)
    
    0 讨论(0)
提交回复
热议问题