我最近实现了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;
}
});
我要去哪里错了?
答案 0 :(得分:0)
除非我弄错了,否则以下内容是错误的:
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;
}
});
您要获取URL的主机,然后将其与完整URL进行比较。那永远是不平等的。
然后仅在该语句为假时才启动链接?这是相反的,因为您在问题中说只有在URL为/valida
时才希望它在浏览器中启动。无论您尝试加载哪个URL,Android都将启动指向/valida
的浏览器。
尝试以下方法:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getPath().contains("valida")) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Toast.makeText(getApplicationContext(), "1Detectou", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});