Sending message through WhatsApp

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

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

     //method used to show IMs
    private void show_custom_chooser(String value) {
        List 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 launchables = pm.queryIntentActivities(email, 0);
        // ////////////new
        list = new ArrayList();
        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();
    
    }
    

提交回复
热议问题