OK, now that we've established that I have zero choice but to construct my
own chooser Intent, what have I missed in the code below?
NB this seems so fragile. I feel like I'm having to make too many
assumptions how how the Android framework works.
/**
> * Creates a Chooser Intent containing all available options including
> the additional one.
> *
> * @param context
> * @param intent Intent describing what to share.
> * @param chooserTitle Title to display on chooser dialog.
> * @param extraIntentTarget Class of the Activity to add as an
> additional share target.
> * @return Chooser Intent outlining all share options.
> */
> public static Intent createChooser(Context context, Intent intent,
> CharSequence chooserTitle, Class extraIntentTarget) {
> // Using a SortedMap to sort the output by name.
> final Comparator displayComparator = new
> PackageItemInfo.DisplayNameComparator(context.getPackageManager());
> final SortedMap<PackageItemInfo, Intent> targetedIntents = new
> TreeMap<PackageItemInfo, Intent>(displayComparator);
> //
> final List<ResolveInfo> resInfo =
> context.getPackageManager().queryIntentActivities(intent, 0);
> for (ResolveInfo resolveInfo : resInfo) {
> final String packageName =
> resolveInfo.activityInfo.packageName;
> if (!resolveInfo.activityInfo.enabled) {
> continue; // Don't include disabled Activities.
> }
> if (!resolveInfo.activityInfo.exported &&
> !context.getPackageName().equals(packageName)) {
> // Don't include non-exported Activities from another
> package
> // This is a work around to
> http://code.google.com/p/android/issues/detail?id=29535
> continue;
> }
> //
> final Intent targetedIntent = new Intent(intent);
> targetedIntent.setPackage(packageName);
> targetedIntents.put(resolveInfo.activityInfo, targetedIntent);
> //
> if (AppConstants.DEV_LOGGING) Log.v(TAG, "#getChooserIntent
> intent=" + targetedIntent);
> }
> //
> // Now add in the extra target for the intent
> final ActivityInfo activityInfo = getActivityInfo(context,
> extraIntentTarget);
> final Intent extraTargetedIntent = new Intent(intent);
> extraTargetedIntent.setClass(context, extraIntentTarget);
> targetedIntents.put(activityInfo, extraTargetedIntent);
> if (AppConstants.DEV_LOGGING) Log.v(TAG, "#getChooserIntent
> extraIntent=" + extraTargetedIntent);
> //
> // Get the Intents so we can split off the first (initialising)
> Intent from the others.
> final List<Intent> orderedIntents = new ArrayList<Intent>();
> orderedIntents.addAll(targetedIntents.values());
> //
> final Intent inialisingIntent =
> orderedIntents.remove(targetedIntents.size() - 1);
> final Intent chooserIntent =
> Intent.createChooser(inialisingIntent, chooserTitle);
> chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
> orderedIntents.toArray(new Parcelable[]{}));
> return chooserIntent;
> }
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en