How can I open a URL in Android's web browser from my application?

前端 未结 30 3280
庸人自扰
庸人自扰 2020-11-21 22:09

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Int         


        
30条回答
  •  没有蜡笔的小新
    2020-11-21 22:44

    Just like the solutions other have written (that work fine), I would like to answer the same thing, but with a tip that I think most would prefer to use.

    In case you wish the app you start to open in a new task, indepandant of your own, instead of staying on the same stack, you can use this code:

    final Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    startActivity(intent);
    

    There is also a way to open the URL in Chrome Custom Tabs . Example in Kotlin :

    @JvmStatic
    fun openWebsite(activity: Activity, websiteUrl: String, useWebBrowserAppAsFallbackIfPossible: Boolean) {
        var websiteUrl = websiteUrl
        if (TextUtils.isEmpty(websiteUrl))
            return
        if (websiteUrl.startsWith("www"))
            websiteUrl = "http://$websiteUrl"
        else if (!websiteUrl.startsWith("http"))
            websiteUrl = "http://www.$websiteUrl"
        val finalWebsiteUrl = websiteUrl
        //https://github.com/GoogleChrome/custom-tabs-client
        val webviewFallback = object : CustomTabActivityHelper.CustomTabFallback {
            override fun openUri(activity: Activity, uri: Uri?) {
                var intent: Intent
                if (useWebBrowserAppAsFallbackIfPossible) {
                    intent = Intent(Intent.ACTION_VIEW, Uri.parse(finalWebsiteUrl))
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
                            or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
                    if (!CollectionUtil.isEmpty(activity.packageManager.queryIntentActivities(intent, 0))) {
                        activity.startActivity(intent)
                        return
                    }
                }
                // open our own Activity to show the URL
                intent = Intent(activity, WebViewActivity::class.java)
                WebViewActivity.prepareIntent(intent, finalWebsiteUrl)
                activity.startActivity(intent)
            }
        }
        val uri = Uri.parse(finalWebsiteUrl)
        val intentBuilder = CustomTabsIntent.Builder()
        val customTabsIntent = intentBuilder.build()
        customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
                or Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
        CustomTabActivityHelper.openCustomTab(activity, customTabsIntent, uri, webviewFallback)
    }
    

提交回复
热议问题