I work in Android Studio and I use WebView to process a web page in my application. I would like to track the URL redirects on this web page so that I can proceed to the next action at the right time.
This URL tracking can be done by overriding the WebViewClient class method "shouldOverrideUrlLoading" so that I can forward a new action for a specific URL. However, there are two implementations of "shouldOverrideUrlLoading":
shouldOverrideUrlLoading(WebView view, String url)
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
The first one (the method ending the string url) is deprecated. The second method shown above only works on API level 21 when I want my application to target API level 15 and above.
I understand that if this is just standard code (not an overriding method), I could pull the API level from the Android phone and then execute any method based on the extracted level. But I'm not sure how to indicate which of these overloaded methods for the user is based on the phone API level.
I also get a red squiggly warning me that calling requires API level 21, but I believe that it will still compile if it is called below API 21?
The following are two versions of the overloaded overloaded method:
This is an obsolete method:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals("test")) {
return true;
}
return false;
}
});
This is a new version of the method where "WebResourceRequest" is only supported at API level 21 +:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.getUrl().equals("test")) {
return true;
}
return false;
}
});
Is there any way to indicate which method to use at certain API levels? Since I'm not sure how to do this without using the deprecated method.