I want to run code only once after an application is opened. I try onpagefinished but when I refresh page the code run again. Sorry for my bad English. This is my code.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
my code
}
Related
Is there a listener that I can know the page is loading or loaded? In general, I know the page is loaded finally by this listener method:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
But when it is paging or paged, there is no any listener infomation I can get. The only changed things are:
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
Is there a way to know whether a h5 page is loading with webview in android platform or I can listen the h5 page's size changed?
I find an another way to solve this problem.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
webView.loadUrl("javascript:(function(){" +
"console.log(\"nodes'number->\"+document.getElementsByTagName(\"*\").length)" +
"})()");
}
}
when the nodes's num is not changed, it is loaded eventually.
Usually, when you click inside Android WebView page and transfer to another page, WebViewClient receives call to shouldOverrideUrlLoading method. But, this doesn't work with money.cnn.com. When I browse on money.cnn.com, I receive callbacks only into onPageStarted and into onPageFinished. Both only once. Browsing inside the site after doesn't give any callbacks to WebViewClient. If I browse only inside the money.cnn.com my log shows only these two lines. No more, no less.
> 10-14 14:10:25.581 28886-28886/com.myapp I/MyApp: onPageStarted
> 10-14 14:10:36.822 28886-28886/com.myapp I/MyApp: onPageFinished
My code is:
mWebView.setWebViewClient(mSWWebViewClient);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl(getUrl());
WebViewClient mSWWebViewClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("MyApp", "onPageStarted ");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("MyApp", "onPageFinished ");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("MyApp", "shouldOverrideUrlLoading ");
}
};
My question is why shouldOverrideUrlLoading doesn't receive callback at all, and how can I detect a change of the content of this site while browsing through it?
Edition
Finally, I have finished with the solution https://stackoverflow.com/a/40551880/955321
I have a HTML template file which is used at multiple places one of them is it is used to get loaded as a webview in few android apps, How can I make sure everytime webview is loaded it loads the most recent version(No cache) without making any changes in app settings.
You can try something like this:
WebView webview = new WebView(this);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Check out more here
When WebView finishes a page everytime, clear the cache. Something like this in your WebViewClient:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.clearCache(true);
}
Just after creating your WebView, before loading any pages, you can clear the cache.
browser.clearCache(true);
and one other way is to Override onPageFinished() which is called each time a page gets loaded, so you could clear cache in it.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.clearCache(true);
}
I have a WebView inside a RecyclerView
I configured the WebViewClient to run onPuase() when page finished loading.
The problem is that some websites (like IMDB) are not viewed, unless I scroll the page down/up, or if the page in stored in cache.
Not working code:
getWebview().setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.onPause();
}
});
If I delay the onPause, it works (delay time differs between different devices)
getWebview().setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
getWebview().postDelayed(new Runnable() {
#Override
public void run() {
getWebview().onPause();
}
}, 5000);
}
});
I also tried getWebview().postInvalidateDelayed() and getWebview().requestLayout().
Is there anyway to force the webview to display the loaded content, or simulate whatever happens when I scroll the page?
I use Lollipop with Android System WebView 43.0.2357.121
If you try to debug or put some logs in onPagefinshed() method, you will come to know that Webview's onPauuse() will call 2-3 times before site the
loads completely in case of URL redirecting.
In webview android I am trying to load a url and in order to check if the load of this url is done successfully (internet connection was available, the server was up etc) I was under the impression that webview.loadUrl would throw exceptions, but wrong! as it explicitly is stated in here "an exception will NOT be thrown".
So how can I check to see if webview.loadUrl did not fail ?
Unfortunately, currently there is no easy way in WebView to ensure that everything on the page has been loaded successfully. We are hoping for a better API to come up in future version. Let me explain what you can do now.
First of all, in order to detect any problems that prevent WebView from making a connection to the server for loading your main page (e.g. bad domain name, I/O error, etc.), you should use WebViewClient.onReceivedError callback as other people correctly suggest:
public class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Make a note about the failed load.
}
}
myWebView.setWebViewClient(new MyWebViewClient());
If the server connection was successful, and the main page was retrieved and parsed, you will receive WebView.onPageFinished callback, so you also need to have this in your WebViewClient subclass:
public class MyWebViewClient extends WebViewClient {
...
#Override
public void onPageFinished(WebView view, String url) {
// Make a note that the page has finished loading.
}
...
}
The caveat here is that if you have received an HTTP error from the server (e.g. a 404 or a 500 error), this callback will be called anyway, it's just the content that you will get in your WebView will be a server error page. People suggest different ways of how to deal with it, see the answers here: How can I check from Android WebView if a page is a "404 page not found"? Basically, it really depends on what you expect to be a "good" page and a "error" page. Unfortunately, there is currently no way for the app to get the HTTP response code from WebView.
The callbacks WebViewClient.onPageStarted and WebViewClient.onProgressChanged are only useful if you want to draw a progress bar as you are loading the page.
Also note that the way of overriding WebViewClient.shouldOverrideUrlLoading that people usually suggest is not correct:
public class MyWebViewClient extends WebViewClient {
...
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// !!! DO NOT DO THIS UNCONDITIONALLY !!!
view.loadUrl(url);
return true;
}
...
}
What few developers realize is that the callback is also called for subframes with non-https schemes. If you'll encounter something like <iframe src='tel:1234'>, you will end up executing view.loadUrl('tel:1234') and your app will show an error page, since WebView doesn't know how to load a tel: URL.
It is recommended to simply return false from the method, if you want WebView to do the loading:
public class MyWebViewClient extends WebViewClient {
...
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Returning 'false' unconditionally is fine.
return false;
}
...
}
This doesn’t mean you should not call WebView.loadUrl from shouldOverrideUrlLoading at all. The specific pattern to avoid is doing so unconditionally for all URLs.
public class AppWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
setProgressBar(true);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//Page load finished
super.onPageFinished(view, url);
setProgressBar(false);
}
}
and then you can do
webView.setWebViewClient(new AppWebViewClient());
For the error part you can override the onReceivedError method
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
Here is what I came up with, it works like a charm.
Boolean failedLoading = false;
WebView webView = view.findViewById(R.id.webView);
webView.loadUrl("www.example.com");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!failedLoading) {
webView.setVisibility(View.VISIBLE);
webView.setAlpha(0f);
ObjectAnimator anim = ObjectAnimator.ofFloat(webView, "alpha",1f);
anim.setDuration(500);
anim.start();
}
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
failedLoading = true;
}
});
It will also work great if you add some kind of a refresh button and then you can call the code above inside a function to try again.
You can check if a URL is loaded successfully by using onProgressChanged()
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
//your url is loaded successfully
}
}
});