Skip to content

Instantly share code, notes, and snippets.

@koocbor
Last active August 29, 2015 14:05
Show Gist options
  • Save koocbor/13b4e641305ef893ccb9 to your computer and use it in GitHub Desktop.
Save koocbor/13b4e641305ef893ccb9 to your computer and use it in GitHub Desktop.
Share Intent for Twitter, Facebook, mms and email
public Intent getShareChooser(String chooserText, final String extraSubject, final String extraLink,
final String emailBody, final String twitterBody) {
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailBody));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, extraSubject);
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, chooserText);
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, twitterBody);
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, extraLink);
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, extraLink);
} else if(packageName.contains("android.gm")) {
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailBody));
intent.putExtra(Intent.EXTRA_SUBJECT, extraSubject);
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
return openInChooser;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment