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 } });
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.
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.
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 .
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);
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With