- Package Com.test.js2java;
- Import Java.util.Timer;
- Import Java.util.TimerTask;
- Import android.app.Activity;
- Import Android.graphics.Bitmap;
- Import Android.os.Bundle;
- Import Android.os.Handler;
- Import Android.os.Message;
- Import Android.util.Log;
- Import Android.view.Window;
- Import android.webkit.WebSettings;
- Import Android.webkit.WebView;
- Import android.webkit.WebViewClient;
- Public class Testjsactivity extends Activity {
- private Long timeout = 5000;
- private WebView Mwebview;
- private Handler Mhandler = new Handler ();
- Private timer timer;
- @Override
- public void OnCreate (Bundle icicle) {
- super.oncreate (icicle);
- Requestwindowfeature (Window.feature_no_title);
- Setcontentview (R.layout.main);
- Mwebview = (WebView) Findviewbyid (R.id.webview);
- WebSettings websettings = Mwebview.getsettings ();
- Websettings.setjavascriptenabled (true);
- Websettings.setallowfileaccess (true);
- Mwebview.setwebviewclient (new Webviewclient () {
- /*
- * Create a webviewclient, rewrite onpagestarted and onpagefinished
- *
- *
- * Start a timer in onpagestarted, and after the set-up time, use handle to send a message to the activity to perform the action after timeout.
- *
- */
- @Override
- public void onpagestarted (WebView view, String URL, Bitmap favicon) {
- LOG.D ("testtimeout", "onpagestarted .....");
- //TODO auto-generated method stub
- super.onpagestarted (view, URL, favicon);
- Timer = new timer ();
- TimerTask TT = new TimerTask () {
- @Override
- public Void Run () {
- /*
- * After the timeout, the first to determine the page load progress, time-out and progress is less than 100, the execution after the timeout action
- */
- if (testjsactivity. This.mWebView.getProgress () < ) {
- LOG.D ("Testtimeout", "Timeout ......");
- Message msg = new Message ();
- Msg.what = 1;
- Mhandler.sendmessage (msg);
- Timer.cancel ();
- Timer.purge ();
- }
- }
- };
- Timer.schedule (TT, timeout, 1);
- }
- /**
- * onpagefinished means page load complete, cancel timer after completion
- */
- @Override
- public void onpagefinished (WebView view, String URL) {
- //TODO auto-generated method stub
- super.onpagefinished (view, URL);
- LOG.D ("Testtimeout", "onpagefinished+++++++++++++++++++++++++");
- LOG.D ("Testtimeout", "+++++++++++++++++++++++++"
- + testjsactivity. this.mWebView.getProgress ());
- Timer.cancel ();
- Timer.purge ();
- }
- });
- Mwebview.loadurl ("http://image.baidu.com/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn= Baiduimage&istype=2&fm=index&pv=&z=0&word=%d7%c0%c3%e6&s=0 ");
- }
- }
It should be noted that the two points of onpagefinished, 1th, the official explanation:
Notify the host application A page has finished loading. This method was called only for main frame. When Onpagefinished () was called, the rendering picture could be updated yet. To get the notification for the new picture, use onNewPicture(WebView, Picture)
.
That is, the program only thinks that the DOM loading is complete, and the picture being loaded is not within this range.
2nd, if in the JS file and dynamic to load another js,onpagefinished method will be all the JS loaded after all the call.
webview--Network Timeout