Qasim Malik
Qasim Malik

Reputation: 75

open specific whatsapp contact from my android app

I want to open a specific contact in my whatsapp by clicking a button I am using this code but it is not working.

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

Please guide me.

Upvotes: 2

Views: 441

Answers (2)

Hridoy Krisna Das
Hridoy Krisna Das

Reputation: 185

I am Using This:

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

Upvotes: 1

Muhammad Sabeel Ahmed
Muhammad Sabeel Ahmed

Reputation: 473

Kindly use below code to achieve this.

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

Upvotes: 3

Related Questions