Skip to content

Instantly share code, notes, and snippets.

@jskierbi
Last active May 20, 2016 11:10
Show Gist options
  • Save jskierbi/9eb472c765e8c8144ba1ae2a3c0e7513 to your computer and use it in GitHub Desktop.
Save jskierbi/9eb472c765e8c8144ba1ae2a3c0e7513 to your computer and use it in GitHub Desktop.
Android share custom contents to different applications
/**
* http://stackoverflow.com/questions/9730243/how-to-filter-specific-apps-for-action-send-intent-and-set-a-different-text-for?answertab=votes#tab-top
*/
public void onShareClick() {
Resources resources = getResources();
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<>();
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;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
Log.d(">>>>", packageName);
if (packageName.contains("email") // Email applicaitons
|| packageName.endsWith(".gm") // Gmail
) {
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
intent.putExtra(Intent.EXTRA_TEXT, "http://google.com/\n\nArticle content, Article content, Article content, Article content, Article content, Article content, Article content, Article content, Article content, Article content");
intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
intent.setType("message/rfc822");
} else {
intent.putExtra(Intent.EXTRA_TEXT, "http://google.com/");
intent.setType("text/plain");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
// convert intentList to array
Intent openInChooser = Intent.createChooser(intentList.get(0), "Choose action");
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new LabeledIntent[intentList.size()]));
startActivity(openInChooser);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment