Try to pick the app to handle this intent. - Android Intent

Android examples for Intent:Open App

Description

Try to pick the app to handle this intent.

Demo Code

/**/*from w  ww .  j  a va 2 s .  com*/
 * (c) Winterwell Associates Ltd, used under MIT License. This file is background IP.
 */
//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import android.util.Log;

public class Main {
    /**
     * Try to pick the app to handle this intent.
     * @param context
     * @param intent
     * @param matchMe E.g. "twitter" Pick the first app whose name contains this.
     * Can be null for pick-anything.
     * @return true if a match was found
     */
    public static boolean pickIntentHandler(Context context, Intent intent,
            String matchMe) {
        final PackageManager pm = context.getPackageManager();
        final List<ResolveInfo> activityList = pm.queryIntentActivities(
                intent, 0);
        List<String> handlers = new ArrayList(activityList.size());
        for (ResolveInfo app : activityList) {
            String name = app.activityInfo.name;
            handlers.add(name);
            if (matchMe == null || name.contains(matchMe)) {
                ActivityInfo activity = app.activityInfo;
                ComponentName compname = new ComponentName(
                        activity.applicationInfo.packageName, activity.name);
                //               intent.addCategory(Intent.CATEGORY_LAUNCHER);
                //               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                intent.setComponent(compname);
                return true;
            }
        }
        Log.d("pick-intent", "No match for " + matchMe + " in " + handlers);
        return false;
    }
}

Related Tutorials