如何以编程方式获取 Android 7.0 运行 设备中已安装的浏览器应用程序列表?

How to get the list of installed Browser Apps in an Android 7.0 running devices programmatically?

在 Android 7.0 之前,我能够检索已安装的浏览器类型应用程序的列表及其包名称。然后,我升级到 Android 7.0,我只能检索三星的互联网浏览器,但不能检索其他浏览器类型的应用程序,例如 Chrome .

设备三星标签 A

这是代码:

public static List<String> getListOfBrowser(Context context) {
    List<String> browserPackageName = new ArrayList<String>();
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> browserList = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo info : browserList) {
            browserPackageName.add(info.activityInfo.packageName);
            Log.e("BrowserList Info ",info.activityInfo.packageName+" total browser"+browserList.size());
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("BrowserList Info ",e.getMessage());
    }
    return browserPackageName;
}

代码看起来不错。但是你应该使用 PackageManager.MATCH_DEFAULT_ONLY 标志 -

pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

而不是

pm.queryIntentActivities(intent, 0);

如果Android API等级>=23,那么你可以这样做:

List<ResolveInfo> browserList;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.MARSHMALLOW) {
    // gets all
    browserList = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
    // only the defaults
    browserList = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
} else {
    browserList = pm.queryIntentActivities(intent, 0);
}