Could not find any Google accoun

2023-08-28  本文已影响0人  wenju
public static void openUrlViaChromeBrowser(Context context, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            List<ResolveInfo> resolveInfos = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                resolveInfos = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_ALL);
            }else{
                resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
            }
            List<String> packageNames = new ArrayList<>();
            String targetPkg = "";
            for (ResolveInfo resolveInfo : resolveInfos) {
                String pkg = resolveInfo.activityInfo.packageName;
                packageNames.add(pkg);
                if (pkg.equals("com.android.chrome")){
                    targetPkg = pkg;
                }else if (pkg.toLowerCase().contains("browser")){
                    targetPkg = pkg;
                }
            }
            if (TextUtils.isEmpty(targetPkg) && !packageNames.isEmpty()){
                targetPkg = packageNames.get(0);
            }
            intent.setPackage(targetPkg);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读