当用户单击外部链接时,我的Webview应用程序就像浏览器一样。我想在默认浏览器中打开该外部链接,并保留我的应用程序链接。这是我的MainActivity代码。任何帮助表示赞赏!
public class MainActivity extends AppCompatActivity {
private WebView webview;
private String urlClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
webview = (WebView) findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
urlClient = "https://google.com";
webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.loadUrl(urlClient);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!Uri.parse(url).getHost().equals("google.com")) {
Uri uri = Uri.parse(url);
startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, uri), "Choose browser"));
return false;
} else {
return true;
}
}
});
}
public void home() {
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
urlClient = "https://google.com";
webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.loadUrl(urlClient);
}
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
}