我最近实现了shouldInterceptRequest方法来检测链接“http://sitemercado.com.br/valida”点击在Android浏览器中打开它而不是在webview内部打开,直到它工作,在浏览器中打开链接但是当我回到webview应用程序时,它也加载了,我希望它只在浏览器中加载 .
我的代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (findViewById(R.id.pb));
mWebView = findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.loadUrl("https://www.sitemercado.com.br/frade");
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Intent i = new Intent(MainActivity.this, off.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.sitemercado.com.br/valida")) {
return true;
}
String valida = "https://www.sitemercado.com.br/valida";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(valida));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return false;
}
});
我哪里错了?
1 回答
除非我弄错了,否则以下是错误的:
您将获得该URL的主机,然后将其与完整URL进行比较 . 这永远不会是平等的 .
那么只有当该语句为假时才启动链接?这是倒置的,因为您在问题中说,只有在URL为
/valida
时才希望它在浏览器中启动 . 您拥有它的方式,无论您尝试加载什么URL,Android都会启动指向/valida
的浏览器 .试试这个: