Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从应用程序在浏览器中打开与webview url不相关的链接_Android_Kotlin - Fatal编程技术网

Android 如何从应用程序在浏览器中打开与webview url不相关的链接

Android 如何从应用程序在浏览器中打开与webview url不相关的链接,android,kotlin,Android,Kotlin,如何在浏览器中打开与webview url无关的外部链接,我正在使用kotlin lang在webview中创建android应用程序。 例如,mydomain.com/链接应在webview中打开 -notmydomain.com/应在浏览器中打开 String url = "http://www.example.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i

如何在浏览器中打开与webview url无关的外部链接,我正在使用kotlin lang在webview中创建android应用程序。 例如,mydomain.com/链接应在webview中打开 -notmydomain.com/应在浏览器中打开

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
顺便说一句,有时检查url是否实际上是网络url也很好,例如,您希望播放应用程序打开的存储url,而不是Webview加载播放存储页面

URLUtil.isNetworkUrl(url)
它适合我,在kotlin中使用它。

试试这个:-

在下面的代码中,我们只加载与特定域匹配的URL,其余的URL将导航到浏览器。您可以根据需要自定义正则表达式。这里我重写了两个方法,因为在api级别21中不推荐使用shouldOverrideUrlLoading

val webview = findViewById<WebView>(R.id.webview)
        webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                val url: String = request?.url.toString()
                if (match(url,"(http|https)://(.*).kotlinlang.org/")) {
                    webview?.loadUrl(url)
                } else {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse(url)
                    startActivity(i)
                }
                return true
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
                if (match(url,"(http|https)://(.*).kotlinlang.org/")) {
                    webview?.loadUrl(url)
                } else {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse(url)
                    startActivity(i)
                }
                return true
            }
        }
        webview.loadUrl("https://kotlinlang.org/")
val webview=findviewbyd(R.id.webview)
webview.webViewClient=对象:webViewClient(){
override fun shouldloverrideUrlLoading(视图:WebView?,请求:WebResourceRequest?):布尔值{
val url:String=request?.url.toString()
if(匹配(url)((http | https):/(.*.kotlinlang.org/)){
webview?.loadUrl(url)
}否则{
val i=意图(意图.行动\视图)
i、 data=Uri.parse(url)
星触觉(一)
}
返回真值
}
override fun shouldOverrideUrlLoading(视图:WebView?,url:字符串):布尔值{
if(匹配(url)((http | https):/(.*.kotlinlang.org/)){
webview?.loadUrl(url)
}否则{
val i=意图(意图.行动\视图)
i、 data=Uri.parse(url)
星触觉(一)
}
返回真值
}
}
webview.loadUrl(“https://kotlinlang.org/")

不要将WebViewClient设置为webview,默认情况下,所有外部链接都重定向到浏览器请显示您的编解码器类MainActivity:AppCompatActivity(){@SuppressLint(“SetJavaScriptEnabled”)重写fun onCreate(savedInstanceState:Bundle?{super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)webview.loadUrl(“)webview.webViewClient=webViewClient()webview.settings.javaScriptEnabled=true WebSettings.LOAD_DEFAULT}我只希望外部打开与我的webview域不相关的链接,例如mywebview域-xyz.com/-所有以xyz.com开头的链接都应该在内部打开。所有与我的webview域不相关的链接(如abc.com/def.com/)都应该在外部打开这甚至可以在浏览器中打开我网站的链接,我只希望外部链接与我的webview无关要从外部打开的站点域。如果(!url.contains(“mydomain.com”),则在启动活动和创建意图之前需要执行字符串筛选。我记得,您还需要从“WebViewCient”中的“shouldOverrideUrlLoading”返回true或false,因为您是否自己处理了链接。是否可以向url.contains或url.startswith添加两个域?我只希望外部打开与我的webview域无关的链接,例如mywebview域-xyz.com/-所有以xyz.com开头的链接都应在内部打开。所有与我的webview域无关的链接(如abc.com/def.com/)都应在外部打开。它在shouldOverrideUrlLoading*val webview=findViewById(R.id.webview)webview.webViewClient=对象:webViewClient(){override fun shouldOverrideUrlLoading]上显示match*和overline的错误(view:WebView?、request:WebResourceRequest?):Boolean{val url:String=request?.url.toString()if(match(url),(http | https://(.*).mysite.com/){WebView?.loadUrl(url)}else{val i=Intent(Intent.ACTION_视图)i.data=Uri.parse(url)startActivity(i)}return true}}以下代码在我的ide上没有显示任何错误,您面临的错误类型是什么,这是语法错误还是运行时错误。请验证您是否为匹配和模式导入java.util.regex.Matcher和import java.util.regex.pattern导入了正确的类。这有帮助吗?:)是否可以将2个域添加到url.contains或url.startswith?
val webview = findViewById<WebView>(R.id.webview)
        webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                val url: String = request?.url.toString()
                if (match(url,"(http|https)://(.*).kotlinlang.org/")) {
                    webview?.loadUrl(url)
                } else {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse(url)
                    startActivity(i)
                }
                return true
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
                if (match(url,"(http|https)://(.*).kotlinlang.org/")) {
                    webview?.loadUrl(url)
                } else {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse(url)
                    startActivity(i)
                }
                return true
            }
        }
        webview.loadUrl("https://kotlinlang.org/")