Is there any way to intercept javascript triggered URL in a WebView
just like normal hrefs using shouldOverrideUrlLoading()
?
setWebViewClient(new WebViewClient() { @Override public void onLoadResource(WebView view, String url) { if (url. startsWith("app://")) { Intent i = new Intent(Intent. ACTION_VIEW, Uri. parse(url), getContext(), Main.
WebView is a special component in Android which serves as kind of built-in browser inside Android applications. If you want to execute HTML, CSS or JavaScript code in your Android app, or you need to allow users visit a URL without leaving your application, WebView is the way to go.
shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.
Android System WebView is a system component for the Android operating system (OS) that enables Android apps to display web content directly inside an application.
onLoadResource()
is called when any resource is being called, including JavaScript. The purpose is a bit different though than shouldOverrideUrlLoading()
.
webControls.setWebViewClient(new WebViewClient() {
//Notify the host application that the WebView will load
//the resource specified by the given url.
@Override
public void onLoadResource (WebView view, String url) {
super.onLoadResource(view, url);
Log.v(TAG, "onLoadResource("+url+");");
}
//Give the host application a chance to take over the
//control when a new url is about to be loaded in the
//current WebView.
@Override
public void shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
Log.v(TAG, "shouldOverrideUrlLoading("+url+");");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With