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

后端 未结 10 1073
情书的邮戳
情书的邮戳 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 03:59

    In google play there is a big variety of chrome browser apps with different features

    So it's correct to check all of them

    fun Context.openChrome(url: String, onError: (() -> Unit)? = null) {
        openBrowser("com.android.chrome", url) {
            openBrowser("com.android.beta", url) {
                openBrowser("com.android.dev", url) {
                    openBrowser("com.android.canary", url) {
                        onError?.invoke() ?: openBrowser(null, url)
                    }
                }
            }
        }
    }
    
    fun Context.openBrowser(packageName: String?, url: String, onError: (() -> Unit)? = null) {
        try {
            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
                setPackage(packageName)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
        } catch (e: ActivityNotFoundException) {
            onError?.invoke()
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:00

    Android open a link in chrome using Java :

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("your url link"));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setPackage("com.android.chrome");
    try {
        context.startActivity(i);
    } catch (ActivityNotFoundException e) {
     Toast.makeText(context, "unable to open chrome", Toast.LENGTH_SHORT).show();
     i.setPackage(null);
     context.startActivity(i);
    }
    

    Android open a link in chrome using Kotlin :

    val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/"))
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    i.setPackage("com.android.chrome")
    try {
      context!!.startActivity(i)
    } catch (e: ActivityNotFoundException) {
      Toast.makeText(context, "unable to open chrome", Toast.LENGTH_SHORT).show()
      i.setPackage(null)
      context!!.startActivity(i)
    }
    
    0 讨论(0)
  • 2020-11-28 04:03

    FOllowing up on @philippe_b's answer, I would like to add that this code will not work if Chrome is not installed. There is one more case in which it will not work - that is the case when Chrome is NOT selected as the default browser (but is installed) OR even if no browser is selected as the default.

    In such cases, add the following catch part of the code also.

    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("http://mysuperwebsite"));
        startActivity(i);
    }
    catch(ActivityNotFoundException e) {
    // 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.parse("somesite.com"));
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
    }
    

    0 讨论(0)
  • 2020-11-28 04:06

    A more elegant way to achieve this is to use the Intent.ACTION_VIEW intent as normal, but add the package com.android.chrome to the intent. This works regardless of whether Chrome is the default browser and ensures exactly the same behavior as if the user had selected Chrome from the chooser list.

    String urlString = "http://mysuperwebsite";
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        // Chrome browser presumably not installed so allow user to choose instead
        intent.setPackage(null);
        context.startActivity(intent);
    }
    

    Update

    For Kindle Devices:

    Just in case if you want to open Amazon Default Browser in case chrome app is not installed in Amazon Kindle

    String urlString = "http://mysuperwebsite";
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        // Chrome browser presumably not installed and open Kindle Browser
        intent.setPackage("com.amazon.cloud9");
        context.startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-11-28 04:12

    This works in Firefox and Opera

    document.location = 'googlechrome://navigate?url=www.example.com/';

    0 讨论(0)
  • 2020-11-28 04:17

    The different answers above are good but none is complete. This in all suited me the best which will :

    try to open chrome web browser and in case exception occurs(chrome is not default or not installed), will ask for choosing the browser from user:

    String uriString = "your uri string";
    
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        Log.d(TAG, "onClick: inTryBrowser");
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        Log.e(TAG, "onClick: in inCatchBrowser", ex );
        intent.setPackage(null);
        startActivity(Intent.createChooser(intent, "Select Browser"));
    }
    
    0 讨论(0)
提交回复
热议问题