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

后端 未结 10 1135
情书的邮戳
情书的邮戳 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()
        }
    }
    

提交回复
热议问题