Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open WhatsApp using an Intent in your Android App

I want an Intent to take control you directly to WhatsApp. So the moment the user clicks on the button, the Intent is supposed to take you to WhatsApp. This is the code I wrote after following a few guide lines but it doesn't work

buttonWhatsapp.setOnClickListener(new View.OnClickListener() {         public void onClick(View v) {             // Performs action on click             Intent sendIntent = new Intent();             sendIntent.setAction(Intent.ACTION_SEND);             sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");             sendIntent.setType("text/plain");             sendIntent.setPackage("com.whatsapp");             startActivity(Intent.createChooser(sendIntent, ""));             startActivity(sendIntent);             //opens the portfolio details class         }     }); 
like image 910
Prateek Mahesh Avatar asked Jul 17 '16 14:07

Prateek Mahesh


People also ask

How do I intent on WhatsApp?

Android intent system Like most social apps on Android, WhatsApp listens to intents to share media and text. Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker: Intent sendIntent = new Intent(); sendIntent.

How do I open an app with intent?

It is possible to start an app's activity by using Intent. setClassName according to the docs. To open it outside the current app, add this flag before starting the intent.

How do I integrate WhatsApp into my app?

Install the WhatsApp Business API Client — Install your API client. Once your client is working, you can update your application settings. Start using the client — Register your phone number with an API call to /account and send a test message with a call to /messages .


2 Answers

Using the 2018 api:

String url = "https://api.whatsapp.com/send?phone="+number; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); 
like image 86
Pablo Cegarra Avatar answered Sep 18 '22 22:09

Pablo Cegarra


This code working for me

String contact = "+00 9876543210"; // use country code with your phone number     String url = "https://api.whatsapp.com/send?phone=" + contact;     try {          PackageManager pm = context.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.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();     e.printStackTrace();     } 
like image 39
Ghanshyam Nayma Avatar answered Sep 18 '22 22:09

Ghanshyam Nayma