Is there any way in Android to force open a link to open in Chrome?

后端 未结 10 1118
情书的邮戳
情书的邮戳 2020-11-28 03:37

I\'m currently testing a webapp developed with lots of jQuery animations, and we\'ve noticed really poor performance with the built-in web browser. While testing in Chrome,

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 04:19

    Here's a more generic approach - it first find outs the package name for the default browser, which handles "http://" URLs, then uses the approach mentioned in the other answers to explicitly open the URL in a browser:

        public void openUrlInBrowser(Context context, String url) {
            // Find out package name of default browser
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
            ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
            String packageName = resolveInfo.activityInfo.packageName;
    
            // Use the explicit browser package name
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            i.setPackage(packageName);
            context.startActivity(i);
        }
    

提交回复
热议问题