Custom Intent-chooser - Why On Android 6 Does It Show Empty Cells?
Background I wish to show the native intent-chooser, while having the ability to customize it a bit. For this, I've found the next StackOverflow thread: How to customize share inte
Solution 1:
I spend some time to read ChooserActivity
and ResolverActivity
and kind of solving thoese problems.
Intentintent=newIntent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty()) {
List<Intent> targetIntents = newArrayList<>();
for (ResolveInfo resolveInfo : resolveInfos) {
ActivityInfoactivityInfo= resolveInfo.activityInfo;
// remove activities which packageName contains 'ttt' for exampleif (activityInfo.packageName.contains("ttt")) {
continue;
}
IntenttargetIntent=newIntent(Intent.ACTION_SEND);
targetIntent.setType("text/plain");
targetIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.setting_share_app_subject));
targetIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.setting_share_app_body));
targetIntent.setPackage(activityInfo.packageName);
targetIntent.setComponent(newComponentName(activityInfo.packageName, activityInfo.name));
// wrap with LabeledIntent to show correct name and iconLabeledIntentlabeledIntent=newLabeledIntent(targetIntent, activityInfo.packageName, resolveInfo.labelRes, resolveInfo.icon);
// add filtered intent to a list
targetIntents.add(labeledIntent);
}
Intent chooserIntent;
// deal with M list seperate problemif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// create chooser with empty intent in M could fix the empty cells problem
chooserIntent = Intent.createChooser(newIntent(), context.getString(R.string.setting_share_app_title));
} else {
// create chooser with one target intent below M
chooserIntent = Intent.createChooser(targetIntents.remove(0), context.getString(R.string.setting_share_app_title));
}
if (chooserIntent == null) {
return;
}
// add initial intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(newParcelable[targetIntents.size()]));
try {
context.startActivity(chooserIntent);
} catch (ActivityNotFoundException e) {
Logger.e(TAG, e, e);
}
}
EDIT: seems on Android Q (10 - API 29) this is broken, and will show just up to 2 items instead of all of them. Asked about this again here.
EDIT: made a sample of choosing which items to exclude from sharing, here, which should work for all Android versions.
Post a Comment for "Custom Intent-chooser - Why On Android 6 Does It Show Empty Cells?"