从我的android应用打开特定的whatsapp联系人

niwlg2el  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(189)

我想在我的whatsapp中打开一个特定的联系人,通过单击一个按钮我正在使用此代码,但它不工作。

btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("smsto:"+"923000000000");
            Intent i = new Intent(Intent.ACTION_SENDTO,uri);
            i.setPackage("com.whatsapp");
            startActivity(i);

请指点。

z31licg0

z31licg01#

请使用下面的代码来实现这一点。

String contact = "+92 3122804640"; // use country code with your phone number
        String url = "https://api.whatsapp.com/send?phone=" + contact;
        try {
            PackageManager pm = getApplicationContext().getPackageManager();
            pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        } catch (PackageManager.NameNotFoundException e) {
            Toast.makeText(MainActivity.this, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
gc0ot86w

gc0ot86w2#

我使用的是:

String phone = "+8801510101010";

            PackageManager packageManager = MainActivity.this.getPackageManager();
            Intent i = new Intent(Intent.ACTION_VIEW);

            try {
                String url = "https://api.whatsapp.com/send?phone="+ phone;
                i.setPackage("com.whatsapp");
                i.setData(Uri.parse(url));
                if (i.resolveActivity(packageManager) != null) {
                    MainActivity.this.startActivity(i);
                }
            } catch (Exception e){
                e.printStackTrace();
            }

相关问题