Java Code Examples for android.content.pm.PackageManager.queryIntentActivities()

The following are Jave code examples for showing how to use queryIntentActivities() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Tusky   File: CustomTabsHelper.java   View Source Code Vote up 7 votes
/**
 * Used to check whether there is a specialized handler for a given intent.
 * @param intent The intent to check with.
 * @return Whether there is a specialized handler for the given intent.
 */
private static boolean hasSpecializedHandlerIntents(Context context, Intent intent) {
    try {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> handlers = pm.queryIntentActivities(
                intent,
                PackageManager.GET_RESOLVED_FILTER);
        if (handlers == null || handlers.size() == 0) {
            return false;
        }
        for (ResolveInfo resolveInfo : handlers) {
            IntentFilter filter = resolveInfo.filter;
            if (filter == null) continue;
            if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) continue;
            if (resolveInfo.activityInfo == null) continue;
            return true;
        }
    } catch (RuntimeException e) {
        Log.e(TAG, "Runtime exception while getting specialized handlers");
    }
    return false;
}
 
Example 2
Project: letv   File: LetvUtils.java   View Source Code Vote up 7 votes
public static boolean openApp(Context context, String isAppPackageName) {
    Intent mainIntent = new Intent("android.intent.action.MAIN", null);
    mainIntent.addCategory("android.intent.category.LAUNCHER");
    PackageManager mPackageManager = BaseApplication.getInstance().getApplicationContext().getPackageManager();
    List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
    Collections.sort(mAllApps, new DisplayNameComparator(mPackageManager));
    for (ResolveInfo res : mAllApps) {
        String pkg = res.activityInfo.packageName;
        String cls = res.activityInfo.name;
        LogInfo.log("Emerson", "-------pkg = " + pkg + "--cls = " + cls);
        if (res.activityInfo.packageName.equals(isAppPackageName)) {
            ComponentName componet = new ComponentName(pkg, cls);
            Intent intent = new Intent();
            intent.setComponent(componet);
            intent.addFlags(268435456);
            context.startActivity(intent);
            return true;
        }
    }
    return false;
}
 
Example 3
Project: ROKOmoji.Emoji.Keyboard.App-Android   File: KeyboardService.java   View Source Code Vote up 6 votes
private ActivityInfo getAppForShare(StickerData stickerData) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    //intent.setType("image/gif");
    intent.setType(stickerData.mime);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo act : activities) {
        ActivityInfo ai = act.activityInfo;
        //Log.d("###",""+editorInfo.packageName+" :: "+ai.applicationInfo.packageName+" | "+ai.name);
        if (editorInfo.packageName.equalsIgnoreCase(ai.applicationInfo.packageName)) {
            return ai;
        }
    }
    return null;
}
 
Example 4
Project: GitHub   File: CustomTabsHelper.java   View Source Code Vote up 6 votes
/**
 * Used to check whether there is a specialized handler for a given intent.
 * @param intent The intent to check with.
 * @return Whether there is a specialized handler for the given intent.
 */
private static boolean hasSpecializedHandlerIntents(Context context, Intent intent) {
    try {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> handlers = pm.queryIntentActivities(
                intent,
                PackageManager.GET_RESOLVED_FILTER);
        if (handlers == null || handlers.size() == 0) {
            return false;
        }
        for (ResolveInfo resolveInfo : handlers) {
            IntentFilter filter = resolveInfo.filter;
            if (filter == null) continue;
            if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) continue;
            if (resolveInfo.activityInfo == null) continue;
            return true;
        }
    } catch (RuntimeException e) {
        Log.e(TAG, "Runtime exception while getting specialized handlers");
    }
    return false;
}
 
Example 5
Project: firefox-tv   File: Browsers.java   View Source Code Vote up 6 votes
private Map<String, ActivityInfo> resolveBrowsers(Context context, PackageManager packageManager, @NonNull Uri uri) {
    final Map<String, ActivityInfo> browsers = new HashMap<>();

    final Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);

    final List<ResolveInfo> infos = packageManager.queryIntentActivities(intent, 0);

    for (ResolveInfo info : infos) {
        if (!context.getPackageName().equals(info.activityInfo.packageName)) {
            browsers.put(info.activityInfo.packageName, info.activityInfo);
        }
    }

    return browsers;
}
 
Example 6
Project: OSchina_resources_android   File: TDevice.java   View Source Code Vote up 5 votes
public static boolean isHaveMarket(Context context) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_MARKET);
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
    return infos.size() > 0;
}
 
Example 7
Project: RxJava2RetrofitDemo   File: WebUtils.java   View Source Code Vote up 5 votes
public static boolean startWithNoWebUrl(Uri uri) {
    String path = uri.getPath();
    if (!TextUtils.isEmpty(path)) {
        Intent intent = FileUtils.buildOpenIntent(uri);
        if (intent != null) { //优先通过外部程序打开
            PackageManager packageManager = getContext().getPackageManager();
            List<ResolveInfo> activityInfoList = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL);
            if (activityInfoList != null && activityInfoList.size() > 0) {
                jumpActivity(intent);
                return true;
            }
        }
    }
    return false;
}
 
Example 8
Project: Paper-Launcher   File: ShortcutsLoader.java   View Source Code Vote up 5 votes
@SuppressLint("WrongConstant")
public static List<ShortcutInfo> loadShortcuts(Context context, String packageName) {
    if (!SDKUtil.AT_LEAST_N_MR1) {
        return null;
    }

    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
    if (launcherApps == null) {
        return null;
    }

    if (!launcherApps.hasShortcutHostPermission()) {
        return null;
    }

    PackageManager packageManager = context.getPackageManager();
    Intent mainIntent = new Intent("android.intent.action.MAIN", null);
    mainIntent.addCategory("android.intent.category.LAUNCHER");

    if (packageManager != null && packageManager.queryIntentActivities(mainIntent, 0) != null) {
        try {
            return launcherApps.getShortcuts((new LauncherApps.ShortcutQuery())
                            .setPackage(packageName).setQueryFlags(QUERY_FLAGS),
                    UserHandle.getUserHandleForUid(context.getPackageManager().getPackageUid(packageName, PackageManager.GET_META_DATA)));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

            return null;
        }
    }

    return null;
}
 
Example 9
Project: GitHub   File: DeviceUtils.java   View Source Code Vote up 5 votes
public static boolean isHaveMarket(Context context) {
    Intent intent = new Intent();
    intent.setAction("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.APP_MARKET");
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
    return infos.size() > 0;
}
 
Example 10
Project: FreeStreams-TVLauncher   File: GetAppList.java   View Source Code Vote up 5 votes
public ArrayList<AppBean> getUninstallAppList() {
    PackageManager localPackageManager = mContext.getPackageManager();
    Intent localIntent = new Intent("android.intent.action.MAIN");
    localIntent.addCategory("android.intent.category.LAUNCHER");
    List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0);
    ArrayList<AppBean> localArrayList = null;
    Iterator<ResolveInfo> localIterator = null;
    if (localList != null) {
        localArrayList = new ArrayList<AppBean>();
        localIterator = localList.iterator();
    }
    while (true) {
        if (!localIterator.hasNext())
            break;
        ResolveInfo localResolveInfo = (ResolveInfo) localIterator.next();
        AppBean localAppBean = new AppBean();
        localAppBean.setIcon(localResolveInfo.activityInfo.loadIcon(localPackageManager));
        localAppBean.setName(localResolveInfo.activityInfo.loadLabel(localPackageManager).toString());
        localAppBean.setPackageName(localResolveInfo.activityInfo.packageName);
        localAppBean.setDataDir(localResolveInfo.activityInfo.applicationInfo.publicSourceDir);
        String pkgName = localResolveInfo.activityInfo.packageName;
        PackageInfo mPackageInfo;
        try {
            mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
            if ((mPackageInfo.applicationInfo.flags & mPackageInfo.applicationInfo.FLAG_SYSTEM) > 0) {//系统预装
                localAppBean.setSysApp(true);
            } else {
                localArrayList.add(localAppBean);
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    return localArrayList;
}
 
Example 11
Project: PlusGram   File: CustomTabsHelper.java   View Source Code Vote up 5 votes
/**
 * 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.
 *
 * 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;
}
 
Example 12
Project: miaosou   File: MiniPayUtils.java   View Source Code Vote up 5 votes
static boolean isActivityAvailable(Context cxt, Intent intent) {
    PackageManager pm = cxt.getPackageManager();
    if (pm == null) {
        return false;
    }
    List<ResolveInfo> list = pm.queryIntentActivities(
            intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list != null && list.size() > 0;
}
 
Example 13
Project: Sega   File: SearchView.java   View Source Code Vote up 5 votes
private boolean isVoiceAvailable() {
    if (isInEditMode()) {
        return true;
    }
    PackageManager pm = getContext().getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    return activities.size() != 0;
}
 
Example 14
Project: siiMobilityAppKit   File: SocialSharing.java   View Source Code Vote up 5 votes
private ActivityInfo getActivity(final CallbackContext callbackContext, final Intent shareIntent, final String appPackageName, final String appName) {
  final PackageManager pm = webView.getContext().getPackageManager();
  List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
  for (final ResolveInfo app : activityList) {
    if ((app.activityInfo.packageName).contains(appPackageName)) {
      if (appName == null || (app.activityInfo.name).contains(appName)) {
        return app.activityInfo;
      }
    }
  }
  // no matching app found
  callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, getShareActivities(activityList)));
  return null;
}
 
Example 15
Project: letv   File: DialogService.java   View Source Code Vote up 5 votes
private List<String> getHomes() {
    List<String> names = new ArrayList();
    PackageManager packageManager = getPackageManager();
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.HOME");
    for (ResolveInfo ri : packageManager.queryIntentActivities(intent, 65536)) {
        names.add(ri.activityInfo.packageName);
    }
    return names;
}
 
Example 16
Project: AndroidUiKit   File: HomeActivity.java   View Source Code Vote up 4 votes
protected List<Map<String, Object>> getData(String prefix) {
    List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(APP_CAGEGORY);

    PackageManager pm = getPackageManager();
    List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

    if (null == list)
        return myData;

    String[] prefixPath;
    String prefixWithSlash = prefix;

    if (prefix.equals("")) {
        prefixPath = null;
    } else {
        prefixPath = prefix.split("/");
        prefixWithSlash = prefix + "/";
    }

    int len = list.size();

    Map<String, Boolean> entries = new HashMap<String, Boolean>();

    for (int i = 0; i < len; i++) {
        ResolveInfo info = list.get(i);
        CharSequence labelSeq = info.loadLabel(pm);
        String label = labelSeq != null
                ? labelSeq.toString()
                : info.activityInfo.name;

        Log.w("yjt","labelSeq = "+labelSeq+"; label="+label+" ; prefixPath="+prefixPath+" ; prefixWithSlash="+prefixWithSlash);
        if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {

            String[] labelPath = label.split("/");

            String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];

            if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
                addItem(myData, nextLabel, activityIntent(
                        info.activityInfo.applicationInfo.packageName,
                        info.activityInfo.name));
            } else {
                if (entries.get(nextLabel) == null) {
                    addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
                    entries.put(nextLabel, true);
                }
            }
        }
    }

    Collections.sort(myData, sDisplayNameComparator);

    return myData;
}
 
Example 17
Project: AndroidCollection   File: MainActivity.java   View Source Code Vote up 4 votes
public List<Map<String, Object>> getData(String prefix) {
    List<Map<String, Object>> list = new ArrayList<>();
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_TEST);

    PackageManager pm = getPackageManager();
    List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
    if (infos == null) return list;

    String[] prefixPath;
    String prefixWithSlash = prefix;
    if (prefix.equals("")) {
        prefixPath = null;
    } else {
        prefixPath = prefix.split("/");
        prefixWithSlash = prefix + "/";
    }
    int len = infos.size();
    Map<String, Boolean> entries = new HashMap<String, Boolean>();

    for (int i = 0; i < len; i++) {
        ResolveInfo info = infos.get(i);
        CharSequence labelSeq = info.loadLabel(pm);
        String label = labelSeq == null ? info.activityInfo.name : labelSeq.toString();
        if(prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)){
            String[] labelPath = label.split("/");
            String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];

            if((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1){
                addItem(list,nextLabel,activityIntent(
                        info.activityInfo.applicationInfo.packageName,
                        info.activityInfo.name));
            }else{
                if (entries.get(nextLabel) == null) {
                    addItem(list, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
                    entries.put(nextLabel, true);
                }
            }
        }
    }

    Collections.sort(list, sDisplayNameComparator);

    return  list;
}
 
Example 18
Project: Bigbang   File: SelectionDbHelper.java   View Source Code Vote up 4 votes
private void queryFilterAppInfo() {

        final PackageManager pm = mContext.getPackageManager();
        // 查询所有已经安装的应用程序
        List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的


        List<AppListAdapter.ApplicationInfoWrap> applicationInfos = new ArrayList<>();
        List<AppListAdapter.ApplicationInfoWrap> allApp = new ArrayList<>();

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        // 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
        List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);

        Set<String> allowPackages = new HashSet();
        for (ResolveInfo resolveInfo : resolveinfoList) {
            allowPackages.add(resolveInfo.activityInfo.packageName);
        }
        for (ApplicationInfo app : appInfos) {
//            if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)//排除系统应用
//            {
//                applicationInfos.add(app);
//            }
//            if(app.uid > 10000){
//                applicationInfos.add(app);
//            }


            AppListAdapter.ApplicationInfoWrap wrap = new AppListAdapter.ApplicationInfoWrap();
            wrap.applicationInfo = app;
            if (allowPackages.contains(app.packageName)) {
                applicationInfos.add(wrap);
            }
            allApp.add(wrap);
        }
        Collections.sort(applicationInfos, new Comparator<AppListAdapter.ApplicationInfoWrap>() {//按名字排序,便于找到应用

            @Override
            public int compare(AppListAdapter.ApplicationInfoWrap lhs, AppListAdapter.ApplicationInfoWrap rhs) {
                // TODO 自动生成的方法存根
                return lhs.applicationInfo.loadLabel(pm).toString().compareToIgnoreCase(rhs.applicationInfo.loadLabel(pm).toString());
            }
        });
        mCanOpenApplicationInfos = applicationInfos;

    }
 
Example 19
Project: GitHub   File: CustomTabsHelper.java   View Source Code Vote up 4 votes
/**
 * 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.
 *
 * 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;
}
 
Example 20
Project: Trinity-App   File: MainActivity.java   View Source Code Vote up 4 votes
private 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;
}