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

后端 未结 10 1074
情书的邮戳
情书的邮戳 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:18

    Here's the closest I've found: Writing a url and replacing http with googlechrome will open Chrome, but it doesn't seem to open the url specified. I'm working with a Samsung Galaxy S5, Android 5.0

    That's the best I've found - every other solution I've seen on SO has required an Android app, not a webapp.

    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2020-11-28 04:20

    All the proposed solutions doesn't work for me anymore. Thanks to @pixelbandito, he pointed me to the right direction. I've found the next constant in the chrome sources

    public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";
    

    And the next usage:

     Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));
    

    So the solution is (note the url should not be encoded)

    void openUrlInChrome(String url) {
        try {
            try {
                Uri uri = Uri.parse("googlechrome://navigate?url="+ url);
                Intent i = new Intent(Intent.ACTION_VIEW, uri);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            } catch (ActivityNotFoundException e) {
                Uri uri = Uri.parse(url);
                // Chrome is probably not installed
                // OR not selected as default browser OR if no Browser is selected as default browser
                Intent i = new Intent(Intent.ACTION_VIEW, uri);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        } catch (Exception ex) {
            Timber.e(ex, null);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:21

    There are two solutions.

    By package

        String url = "http://www.example.com";
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setPackage("com.android.chrome");
        try {
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Chrome is probably not installed
            // Try with the default browser
            i.setPackage(null);
            startActivity(i);
        }
    

    By scheme

        String url = "http://www.example.com";
        try {
            Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Chrome is probably not installed
        }
    

    WARNING! The following technique does not work on most recent versions of Android. It is here for reference, because this solution has been around for a while:

        String url = "http://www.example.com";
        try {
            Intent i = new Intent("android.intent.action.MAIN");
            i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
            i.addCategory("android.intent.category.LAUNCHER");
            i.setData(Uri.parse(url));
            startActivity(i);
        }
        catch(ActivityNotFoundException e) {
            // Chrome is probably not installed
        }
    
    0 讨论(0)
提交回复
热议问题