如何在浏览器中而不是在webview中打开其他链接



你好,我有一个webview android应用程序。我希望我的网站只在webview中打开,如果有人点击了与我的网站无关的url,那么就在浏览器中打开。例如:如果我的网站是little.com,如果有人点击了facebook.com或google.com的链接,那么facebbok或谷歌会在浏览器中打开。

androidmainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.little.example">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- The below is for the splash screen and we need no action bar and the default theme -->
<activity android:name=".homeActivity"
android:theme="@style/AppTheme.NoActionBar"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name=".MainActivity">
</activity>
</application>
</manifest>

主要活动.kt

package com.little.example
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.webkit.WebBackForwardList
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.little.example.R
import kotlinx.android.synthetic.main.activity_main.*
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webview.settings.javaScriptEnabled = true
webview.loadUrl("https://little.com")
webview.webViewClient = MyWebViewClient()

}
override fun onBackPressed() {
if (webview.canGoBack()){
webview.goBack()
}
else {
super.onBackPressed()
}
}
private inner class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (!url.contains("little.com")) {//for example
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
view.loadUrl(url)
return false
}}
}

您可以根据需要处理每个url,只需通过自定义WebViewClient 设置您的webview

mWebView.webViewClient = MyWebViewClient()
}
private inner class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (!url.contains("little.com")) {//for example
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
mWebView.loadUrl(url)
return false
Intent browserIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("http://www.google.com"));
startActivity(browserIntent);

您可以使用此代码在浏览器中打开页面

通过在androidx 中使用自定义选项卡

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(context,Uri.parse("http://www.google.com"));

最新更新