I have some logic I want my WebView to apply whenever I try to change its url. For links within loaded pages it's fine, I can just do it by using shouldOverrideUrlLoading method on my custom WebViewClient.
But when I explicitly call loadUrl on my WebView, shouldOverrideUrlLoading is not fired (obviously, or it would produce an endless loop when calling loadUrl from within this method).
Is there a way to "preload" a url manually so the shouldOverrideUrlLoading method is called?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
This works fine when I click a link from within the rendered page. But if I call:
mWebView.loadUrl("myapp://whatever");
shouldOverrideUrlLoading() is not called. What I am asking is whether there's a way that I can pass a url to my webView so it gets passed through to shouldOVerrideUrlLoading() before actually loading it. (i.e. shouldOVerrideUrlLoading() is called whenever a new url is about to be loaded. That's the event I want to fire)
REF: Android's official WebView documentation
if your app version greater then 'N' you should use this function
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
check you function and version
If all you're trying to do is re-use your logic, just extract it into your WebViewClient and use that to load the url and then just delegate to this in shouldOverrideUrlLoading():
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(view, url);
}
public boolean loadUrl(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
Then instead of
mWebView.loadUrl("myapp://whatever");
You do:
mWebViewClient.loadUrl(mWebView, "myapp://whatever");
Related
There is a WebView in my android app. It loads a webpage and user may click links to launch other apps by url scheme. Now I need to avoid this action.
For example, when user click a correct deeplink or applink in WeChat, WeChat will just say something like "sorry we can not launch xxx..." in its WebView, and user can open that page in browser to jump to the target app. I want to do the same thing.
I have seen this similar question how-can-i-disable-deeplinks in SO but there is no useful answer. And I have also tried to do something in shouldOverrideUrlLoading or shouldInterceptRequest but neither works. That means I can recognize the special url in shouldOverrideUrlLoading, but override its return to true or false can not stop launching app because the deeplink action is not an override in WebView.
url.startsWith("intent://")
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("intent://")) {
// This is a deeplink URL, return true to disable deeplinking
Toast.makeText(requireActivity(), "sorry we can not launch: "+url, Toast.LENGTH_SHORT).show();
return true;
} else {
// This is not a deeplink URL, let the WebView handle it
return super.shouldOverrideUrlLoading(view, url);
}
}
Add something like this to your WebViewClient:
/**
* The shouldOverrideUrlLoading(WebView view, String url) method is deprecated in API 24 and
* the shouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is added in API 24.
* If you are targeting both older versions of Android and compiling with 24+, you need both methods.
* If not, you can delete the deprecated one
*/
#SuppressWarnings("deprecation") // Needs SDK 24 to be removed
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return handleUrl(Uri.parse(url));
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return handleUrl(request.getUrl());
}
private boolean handleUrl(Uri uri) {
// This can become more complex to support other url schemes like ftp
return !uri.getScheme().startsWith("http");
}
The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).
I have a custom WebViewClient, in which I have the following method :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String modifiedUrl = Util.someMethod(url);
super.shouldOverrideUrlLoading(view, modifiedUrl);
}
Will it work or should I put this logic in another method, for example onPagestarted ?
You rather should do something like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(conditionForModifyingUrl){
String modifiedUrl = Util.someMethod(url);
view.loadUrl(modifiedUrl);
return true;
}
return false;
}
Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.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.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
WebViewClient Reference
Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
But it must return false otherwise, so:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.startsWith(myString){
// handle by yourself
return true;
}
// ...
return false;
}
I'm using webview in android
and when I call loadURL it does a call to the browser activity
I need to show the webcontent inside my own activity as I have other stuff to be viewed in it
buttons, text, ,.. etc
Use the WebView.setWebViewClient(WebViewClient) method ( http://developer.android.com/reference/android/webkit/WebView.html) and override the shouldOverrideUrlLoading(WebView, String) method like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ( url.equals( "http://www.example.com" ) ) {
// Do something
return true;
}
// If we couldn't match the URL, the following line
// lets the webview use default behaviour (i.e. open browser)
return super.shouldOverrideUrlLoading(view, url);
}
});
I've defined a private class that extends WebViewClient and set my WebView's client to an instance of that class (webView01.setWebViewClient(new Callback());).
The class definition is as follows:
private class Callback extends WebViewClient {
public void onLoadResource (WebView view, String url) {
}
public void onPageStarted (WebView view, String url, Bitmap favicon) {
}
public void onPageFinished (WebView view, String url) {
Animation anim = AnimationUtils.loadAnimation(MyNews.this, R.anim.webviewanim);
view.startAnimation(anim);
}
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
Log.e("loading url", url);
return false;
}
}
My problem is that onPageFinished is definitely getting called, but shouldOverrideUrlLoading is never being called.
What am I doing wrong here?
6/13/2010
A little more information.
I'm loading the initial page from a string ( webView01.loadDataWithBaseURL("fake://fake", myHTML, "text/html", "UTF-8", null);
).
The page loads (and there are several calls to onLoadResource) and onPageFinished is being called at page load completion.
When the user taps on an Anchor link in the page, instead of calling shouldOverrideUrlLoading, onLoadResource is called.
I need to invoke the full web browser when the user taps on the link, but I never get the chance to override url loading.
From what i have featured in webview till now, the shouldOverrideUrlLoading() doesn't get called when you call webview.load("website"), but if this website has redirection to another it works.
So the work around is to do what you want to be done when you call webview.load("website") (like calling a function that writes to the log Log.e("loading url", url)) since you already know the url and what needs to be done, shouldOverrideUrlLoading() only intercepts the requests sent from the webview it self.
Hope i was helpful.