ActivityPicker.javaAPI DocAndroid 1.5 API15377Wed May 06 22:42:48 BST


public class ActivityPicker extends implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener
Displays a list of all activities matching the incoming {@link Intent#EXTRA_INTENT} query, along with any injected items.

Fields Summary
private PickAdapter
Adapter of items that are displayed in this dialog.
private android.content.Intent
Base {@link Intent} used when building list.
Constructors Summary
Methods Summary
protected android.content.IntentgetIntentForPosition(int position)
Build the specific {@link Intent} for a given list position. Convenience method that calls through to {@link PickAdapter.Item#getIntent(Intent)}.

        PickAdapter.Item item = (PickAdapter.Item) mAdapter.getItem(position);
        return item.getIntent(mBaseIntent);
protected java.util.ListgetItems()
Build and return list of items to be shown in dialog. Default implementation mixes activities matching {@link #mBaseIntent} from {@link #putIntentItems(Intent, List)} with any injected items from {@link Intent#EXTRA_SHORTCUT_NAME}. Override this method in subclasses to change the items shown.

        PackageManager packageManager = getPackageManager();
        List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
        // Add any injected pick items
        final Intent intent = getIntent();
        ArrayList<String> labels =
        ArrayList<ShortcutIconResource> icons =
        if (labels != null && icons != null && labels.size() == icons.size()) {
            for (int i = 0; i < labels.size(); i++) {
                String label = labels.get(i);
                Drawable icon = null;
                try {
                    // Try loading icon from requested package
                    ShortcutIconResource iconResource = icons.get(i);
                    Resources res = packageManager.getResourcesForApplication(
                    icon = res.getDrawable(res.getIdentifier(
                            iconResource.resourceName, null, null));
                } catch (NameNotFoundException e) {
                items.add(new PickAdapter.Item(this, label, icon));

        // Add any intent items if base was given
        if (mBaseIntent != null) {
            putIntentItems(mBaseIntent, items);
        return items;
public voidonCancel(android.content.DialogInterface dialog)
Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.

public voidonClick(android.content.DialogInterface dialog, int which)
Handle clicking of dialog item by passing back {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.

        Intent intent = getIntentForPosition(which);
        setResult(Activity.RESULT_OK, intent);
protected voidonCreate(android.os.Bundle savedInstanceState)

        final Intent intent = getIntent();
        // Read base intent from extras, otherwise assume default
        Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
        if (parcel instanceof Intent) {
            mBaseIntent = (Intent) parcel;
        } else {
            mBaseIntent = new Intent(Intent.ACTION_MAIN, null);

        // Create dialog parameters
        AlertController.AlertParams params = mAlertParams;
        params.mOnClickListener = this;
        params.mOnCancelListener = this;
        // Use custom title if provided, otherwise default window title
        if (intent.hasExtra(Intent.EXTRA_TITLE)) {
            params.mTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
        } else {
            params.mTitle = getTitle();
        // Build list adapter of pickable items
        List<PickAdapter.Item> items = getItems();
        mAdapter = new PickAdapter(this, items);
        params.mAdapter = mAdapter;

protected voidputIntentItems(android.content.Intent baseIntent, java.util.List items)
Fill the given list with any activities matching the base {@link Intent}.

        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(baseIntent,
                0 /* no flags */);
        Collections.sort(list, new ResolveInfo.DisplayNameComparator(packageManager));
        final int listSize = list.size();
        for (int i = 0; i < listSize; i++) {
            ResolveInfo resolveInfo = list.get(i);
            items.add(new PickAdapter.Item(this, packageManager, resolveInfo));