Reputation: 4713
In my android application I have 4 buttons for Facebook, Viber, Telegram and Whatsapp and I want to share a different content based on each button.
For example if the user clicks on viber button I want to user ACTION_SEND to share the content with viber only.
I found THIS which explains how to do this for facebook and twitter but it seems it's calling a specific class name of that application which I don't know what it would be for the applications I wanna use except facebook.
Upvotes: 1
Views: 1199
Reputation: 1001
All android apps have unique id, so first we have to check if these apps are installed in user's device and then we can pass the unique id via intent for sharing. Do according below:
Unique ids for different apps :
Viber : com.viber.voip
Telegram : org.telegram.messenger
Whatsapp : com.whatsapp
Check if these apps are installed and if installed then send messages through intent.
private void sendMessage(Context context,String message, String appIds)
{
final boolean isAppInstalled =isAppAvailable(context, appIds);
if (isAppInstalled)
{
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage(appIds);
myIntent.putExtra(Intent.EXTRA_TEXT, message);
mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
}
else
{
Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show();
}
}
Indicates whether the specified app installed and can used as an intent. This method checks the package manager for installed packages that can respond to an intent with the specified app. If no suitable package is found, this method returns false.
private boolean isAppAvailable(Context context, String appName)
{
PackageManager pm = context.getPackageManager();
try
{
pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
return true;
}
catch (NameNotFoundException e)
{
return false;
}
}
Upvotes: 3