Sending message through WhatsApp

前端 未结 23 2208
萌比男神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:26

    I'm really late here but I believe that nowadays we have shorter and better solutions to send messages through WhatsApp.

    You can use the following to call the system picker, then choose which app you will use to share whatever you want.

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

    If you are really need to send through WhatsApp all you need to do is the following (You will skip the system picker)

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

    If you need more information you can find it here: WhatsApp FAQ

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

    Simple solution, try this.

    String phoneNumberWithCountryCode = "+62820000000";
    String message = "Hallo";
    
    startActivity(
        new Intent(Intent.ACTION_VIEW,
            Uri.parse(
                String.format("https://api.whatsapp.com/send?phone=%s&text=%s", phoneNumberWithCountryCode, message)
            )
        )
    );
    
    0 讨论(0)
  • 2020-11-22 10:30

    this is much lengthy but surly working. enjoy your code:)

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

    use this singleline code use to Sending message through WhatsApp

    //NOTE : please use with country code first 2digits without plus signed
    try {
          String mobile = "911234567890";
          String msg = "Its Working";
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=" + mobile + "&text=" + msg)));
          }catch (Exception e){
            //whatsapp app not install
         }
    
    0 讨论(0)
  • 2020-11-22 10:32

    get the contact number whom you want to send the message and create uri for whatsapp, here c is a Cursor returning the selected contact.

    Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
    i.setType("text/plain");
    i.setPackage("com.whatsapp");           // so that only Whatsapp reacts and not the chooser
    i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
    startActivity(i);
    
    0 讨论(0)
提交回复
热议问题