I’m using sendIntent in my anroid project to send the user to whatsapp for sending the message. But i want both apps whatsapp and business whatsapp, so user can select according to there preference. Here is the code i tried :
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" + toNumber));
sendIntent.setPackage("com.whatsapp","com.whatsapp.w4b");
startActivity(sendIntent);
This doesn’t work. I also tried this code
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" + toNumber));
sendIntent.setPackage("com.whatsapp");
sendIntent.setPackage("com.whatsapp.w4b");
startActivity(sendIntent);
This works but this code autmatically open businesswhatsapp.
>Solution :
The reason why the first code snippet you tried did not work is because setPackage method only takes one package name at a time, so you cannot specify multiple package names like you did.
The second code snippet you tried only specifies the second package name, so it will always open Business WhatsApp.
One way to allow the user to choose between WhatsApp and Business WhatsApp would be to present a chooser dialog that lets the user select which app to use. You can do this by using the Intent.createChooser() method, like this:
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + toNumber));
sendIntent.setPackage("com.whatsapp");
Intent businessSendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + toNumber));
businessSendIntent.setPackage("com.whatsapp.w4b");
Intent chooserIntent = Intent.createChooser(sendIntent, "Choose app");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { businessSendIntent });
startActivity(chooserIntent);
In this code, we create two separate intents, one for WhatsApp and one for Business WhatsApp. Then, we create a chooser intent using Intent.createChooser(), which will display a dialog that allows the user to choose which app to use. We set the main intent (sendIntent) as the initial option, and add the businessSendIntent as an additional option using the EXTRA_INITIAL_INTENTS extra.
This way, the user can choose which app to use, and the selected app will be used to send the message.