OnPageFinished quits before page loads

I just want to know when the html page loads. OnPageFinished is called before the entire page loads. I got a blank view when I installed Image View using a bitmap created from WebView

mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setAlwaysDrawnWithCacheEnabled(true); mWebView.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon){ super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view,String url){ Bitmap b = Bitmap.createBitmap( 480, 240, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()); view.draw(c); mImageView.setImageBitmap(b); //b.recycle(); } }); 

EDIT:

I tried to implement JavaScriptInterface now only with the creation of a Toast post, but it still doesn't work. I pasted the html code and Java:

EDIT:

Finally, I started to work. I used JavaScriptInterface with the correct one. When loadUrl starts, the html file is loaded. When the entire page is accessible on the screen, the onload function of the html file calls the captureImage function from the JavaScriptInterface class. Now Iโ€™m sure that the page is fully loaded and I can view it. Thanks for the help.

CODE:

JAVA:

  mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setDrawingCacheEnabled(true); mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); mWebView.setWebViewClient(new WebViewClient(){}); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mWebView.loadUrl("file:///android_asset/html_sample.html"); //mWebView.loadUrl("https://www.onet.pl"); } }); public class JavaScriptInterface { private Context mContext;; public JavaScriptInterface(Context context) { mContext = context; } @JavascriptInterface public void captureImage(){ Bitmap b = mWebView.getDrawingCache(true); mImageView.setImageBitmap(b); } @JavascriptInterface public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } } 

HTML:

 <html> <head> <title>My first styled page</title> <link rel="stylesheet" href="sample_css.css"> </head> <body onload="function(){ Android.captureImage() }"> ... </body> </html> 
+6
source share
4 answers
 try this :- OnClickListener btnGoListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub webView00.loadUrl(etUrl.getText().toString()); webView00.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { Toast.makeText(getBaseContext(), "Loading Finished 2", Toast.LENGTH_LONG) .show(); } }); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_web_page00); webView00 = (WebView) findViewById(R.id.webView00); btnGo = (Button) findViewById(R.id.go_btn); etUrl = (EditText) findViewById(R.id.url_edittext); webView00.getSettings().setJavaScriptEnabled(true); btnGo.setOnClickListener(btnGoListener); webView00.loadUrl("http://google.com"); //webView00.setWebViewClient(new myWebViewClient()); //webView00.setWebChromeClient(new myWebChromeClient()); webView00.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { Toast.makeText(getBaseContext(), "Loading Finished 1", Toast.LENGTH_LONG) .show(); } }); } 
0
source

This is because onPageFinished means that the page has finished loading, and not โ€œpage on screenโ€. Unfortunately, WebView does not have the "page you want, now on the" API screen. Typically, people resort to a combination of PictureListener (which has been deprecated since unreliability) and random timeouts (which are too large or do not work on some devices).

You can try using a rendering with a small bitmap and check if it is all white on each PictureListener (and don't forget to stop doing this when you finish waiting, otherwise you will write tons of CPUs if the page is animated / GIFs / etc. .d ..). Rendering a small bitmap image will still be very intense if there are large images on the page, so this is certainly not a good solution.

+5
source

Use the WebViewClient method and onPageFinished , as shown below:

 mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) { if (!loadingFinished) { redirect = true; } loadingFinished = false; webView.loadUrl(urlNewString); return true; } @Override public void onPageStarted(WebView view, String url) { loadingFinished = false; //SHOW LOADING IF IT ISNT ALREADY VISIBLE } @Override public void onPageFinished(WebView view, String url) { if(!redirect){ loadingFinished = true; } if(loadingFinished && !redirect){ Bitmap b = Bitmap.createBitmap( 480, 240, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()); view.draw(c); mImageView.setImageBitmap(b); } else{ redirect = false; } } }); 

For more information see fooobar.com/questions/19756 / ...

Hope this helps.

EDIT:. According to your comment, I think your page loads successfully, but it takes time to render / draw the page on the screen, which is the cause of your problem. Try implementing onPictureListener for web browsing. For more information see fooobar.com/questions/971862 / ...

0
source

It also happened to me that in the WebViewClient, the onPageFinished callback is fired before the actual window.load event.

It is still being investigated why this happens sometimes, which is difficult because the Android application was created by a specially tuned built-in hybrid application generator.

However, I walked past this issue by performing a protection that checks to see if there is an error in the script that I want to call onPageFinished, in which case I add an event handler that calls my script again during window.load.

You can also use document.addEventListener ("DOMContentLoaded", ...) instead of window.load.

Below is a snippet of code that illustrates this, as well as debug.log calls. You can pass a call to your javascript interface as an argument

  private void executeOnLoad(final WebView view, final String jsStringToExecute) { StringBuilder sb = new StringBuilder("javascript:"); sb.append("try {console.log('native.onPageFinished. document.readyState: ' + document.readyState);").append(jsStringToExecute).append("} catch(err){console.log('error calling jsBridge script on page load, deferring call to window.load event');") .append("window.addEventListener('load', function() {console.log('window.onload callled from native. document.readyState: ' + document.readyState);").append(jsStringToExecute).append("});").append("}"); String jsWithWindowLoadedFallback = sb.toString(); view.loadUrl(jsWithWindowLoadedFallback); } 
0
source

Source: https://habr.com/ru/post/971860/


All Articles