Example usage for android.content.pm PackageManager queryIntentActivities

List of usage examples for android.content.pm PackageManager queryIntentActivities

Introduction

In this page you can find the example usage for android.content.pm PackageManager queryIntentActivities.

Prototype

public abstract List<ResolveInfo> queryIntentActivities(Intent intent, @ResolveInfoFlags int flags);

Source Link

Document

Retrieve all activities that can be performed for the given intent.

Usage

From source file:org.totschnig.myexpenses.util.Utils.java

/**
 * Indicates whether the specified action can be used as an intent. This
 * method queries the package manager for installed packages that can
 * respond to an intent with the specified action. If no suitable package is
 * found, this method returns false.//from w  w  w .  j  a v  a  2s. c o  m
 *
 * From http://android-developers.blogspot.fr/2009/01/can-i-use-this-intent.html
 *
 * @param context The application's environment.
 * @param action The Intent action to check for availability.
 *
 * @return True if an Intent with the specified action can be sent and
 *         responded to, false otherwise.
 */
public static boolean isIntentAvailable(Context context, Intent intent) {
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

From source file:de.janrenz.app.mediathek.LiveFragment.java

public static boolean canDisplayM3u8(Context context) {
    if (context != null) {
        return false;
    }/*from w ww .  j  av a 2  s  .co  m*/
    try {
        PackageManager packageManager = context.getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/x-mpegURL");
        if (packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
            return true;
        }
    } catch (Exception e) {

    }
    return false;
}

From source file:org.alfresco.mobile.android.application.managers.ActionUtils.java

/**
 * Open Play Store application or its web version if no play store
 * available./*from  w w  w  .  ja v a  2 s  .c  o m*/
 * 
 * @param c : Android Context
 */
public static void actionDisplayPlayStore(Context c) {
    // Retrieve list of application that understand market Intent
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://details?type=org.alfresco.mobile.android.application"));
    final PackageManager mgr = c.getPackageManager();
    List<ResolveInfo> list = mgr.queryIntentActivities(intent, 0);

    // By default we redirect to the webbrowser version of play store.
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("https://play.google.com/"));

    for (ResolveInfo resolveInfo : list) {
        // If we find something related to android we open the application
        // version of play store.
        if (resolveInfo.activityInfo.applicationInfo.packageName.contains("android")) {
            intent.setComponent(new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
                    resolveInfo.activityInfo.name));
            intent.setData(Uri.parse("market://"));
            break;
        }
    }
    c.startActivity(intent);
}

From source file:com.bangalore.barcamp.BCBUtils.java

public static void addNavigationActions(final BCBActivityBaseClass homeActivity) {
    homeActivity.setBehindContentView(R.layout.navigation_menu);
    int offset = 100;
    DisplayMetrics metrics = new DisplayMetrics();
    homeActivity.getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    offset = ((metrics.widthPixels * 130)) / 480;

    homeActivity.setBehindOffset(offset);
    homeActivity.setBehindScrollScale(0.5f);

    View view = homeActivity.findViewById(R.id.nav_agenda);
    view.setOnClickListener(new OnClickListener() {

        @Override/*ww w. j  a  v  a  2 s .  c o m*/
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, ScheduleActivity.class);
            homeActivity.startActivityForResult(intent, START_SCHEDULE);

        }
    });

    view = homeActivity.findViewById(R.id.nav_about);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, AboutActivity.class);
            homeActivity.startActivityForResult(intent, START_ABOUT);
        }
    });
    view = homeActivity.findViewById(R.id.nav_internal_venue_map);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, InternalVenueMapActivity.class);
            homeActivity.startActivityForResult(intent, START_INTERNAL_VENUE);
        }
    });

    view = homeActivity.findViewById(R.id.nav_settings);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, SettingsActivity.class);
            homeActivity.startActivityForResult(intent, START_SETTINGS);
        }
    });
    view.setVisibility(View.GONE);
    view = homeActivity.findViewById(R.id.nav_share);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, ShareActivity.class);
            homeActivity.startActivityForResult(intent, START_SHARE);
        }
    });
    view = homeActivity.findViewById(R.id.nav_tweets);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, WebViewActivity.class);
            intent.putExtra(WebViewActivity.URL, "file:///android_asset/bcb11_updates.html");
            homeActivity.startActivityForResult(intent, START_BCB12_TWEETS);
        }
    });
    view = homeActivity.findViewById(R.id.nav_update);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(homeActivity, UpdateMessagesActivity.class);
            homeActivity.startActivityForResult(intent, START_BCB_UPDATES);
        }
    });
    view = homeActivity.findViewById(R.id.nav_venue);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final PackageManager pm = homeActivity.getPackageManager();

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(BCB_LOCATION_MAPS_URL));
            final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
            for (ResolveInfo info : matches) {
                Log.e("MapPackage", info.loadLabel(pm) + " " + info.activityInfo.packageName + " "
                        + info.activityInfo.name);
                if (info.activityInfo.name.equals("com.google.android.maps.MapsActivity")) {
                    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                }
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            homeActivity.startActivity(intent);
        }
    });
    view = homeActivity.findViewById(R.id.nav_BCB);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://barcampbangalore.org"));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            homeActivity.startActivity(intent);
        }
    });

}

From source file:at.diamonddogs.util.Utils.java

/**
 * Brings up the MAIN/LAUNCHER activity and clears the top
 *
 * @param context a {@link Context}/*from   w  ww.  jav a2 s .  c o  m*/
 */
public static void returnToHome(Context context) {
    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setPackage(context.getPackageName());
    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    Intent homeIntent = new Intent("android.intent.action.MAIN");
    homeIntent.addCategory("android.intent.category.LAUNCHER");
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    homeIntent.setComponent(new ComponentName(context.getPackageName(), activities.get(0).activityInfo.name));
    context.startActivity(homeIntent);
}

From source file:com.xlythe.engine.theme.Theme.java

/**
 * Returns a list of installed apps that are registered as themes
 *//*  w  w w  . ja va2 s . c om*/
public static List<App> getApps(Context context) {
    LinkedList<App> apps = new LinkedList<App>();
    PackageManager manager = context.getPackageManager();

    Intent mainIntent = new Intent(context.getPackageName() + ".THEME", null);

    final List<ResolveInfo> infos;
    try {
        infos = manager.queryIntentActivities(mainIntent, 0);
    } catch (Exception e) {
        e.printStackTrace();
        return apps;
    }

    for (ResolveInfo info : infos) {
        App app = new App();
        apps.add(app);

        app.setName(info.loadLabel(manager).toString());
        app.setPackageName(info.activityInfo.applicationInfo.packageName);
    }

    return apps;
}

From source file:Main.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.//from   w w w.ja  v  a2 s  .com
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:com.elkriefy.android.apps.chubbytabby.CustomTabsHelper.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks the one
 * chosen by the user if there is one, otherwise makes a best effort to return a valid package
 * name.//www. j  a  v a2 s .  c  om
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.packtpub.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!android.text.TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:biz.bokhorst.xprivacy.Util.java

public static boolean isIntentAvailable(Context context, Intent intent) {
    PackageManager packageManager = context.getPackageManager();
    return (packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES).size() > 0);
}

From source file:org.totschnig.myexpenses.Utils.java

static void share(Context context, File file, String target) {
    URI uri = null;/*from  w  ww  . j ava 2s .co  m*/
    try {
        uri = new URI(target);
    } catch (URISyntaxException e1) {
        Toast.makeText(context, context.getString(R.string.ftp_uri_malformed, target), Toast.LENGTH_LONG)
                .show();
        return;
    }
    String scheme = uri.getScheme();
    if (scheme.equals("ftp")) {
        new Utils.FtpAsyncTask(context, file, uri).execute();
        return;
    } else if (scheme.equals("mailto")) {
        final PackageManager packageManager = context.getPackageManager();
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("text/qif");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { uri.getSchemeSpecificPart() });
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Expenses export");
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        if (packageManager.queryIntentActivities(emailIntent, 0).size() == 0) {
            Toast.makeText(context, R.string.no_app_handling_email_available, Toast.LENGTH_LONG).show();
            return;
        }

        context.startActivity(emailIntent);
    } else {
        Toast.makeText(context, context.getString(R.string.share_scheme_not_supported, target),
                Toast.LENGTH_LONG).show();
    }
}