webview--Network Timeout

Source: Internet
Author: User

  1. Package Com.test.js2java;
  2. Import Java.util.Timer;
  3. Import Java.util.TimerTask;
  4. Import android.app.Activity;
  5. Import Android.graphics.Bitmap;
  6. Import Android.os.Bundle;
  7. Import Android.os.Handler;
  8. Import Android.os.Message;
  9. Import Android.util.Log;
  10. Import Android.view.Window;
  11. Import android.webkit.WebSettings;
  12. Import Android.webkit.WebView;
  13. Import android.webkit.WebViewClient;
  14. Public class Testjsactivity extends Activity {
  15. private Long timeout = 5000;
  16. private WebView Mwebview;
  17. private Handler Mhandler = new Handler ();
  18. Private timer timer;
  19. @Override
  20. public void OnCreate (Bundle icicle) {
  21. super.oncreate (icicle);
  22. Requestwindowfeature (Window.feature_no_title);
  23. Setcontentview (R.layout.main);
  24. Mwebview = (WebView) Findviewbyid (R.id.webview);
  25. WebSettings websettings = Mwebview.getsettings ();
  26. Websettings.setjavascriptenabled (true);
  27. Websettings.setallowfileaccess (true);
  28. Mwebview.setwebviewclient (new Webviewclient () {
  29. /* 
  30. * Create a webviewclient, rewrite onpagestarted and onpagefinished
  31. *
  32. *
  33. * 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.
  34. *
  35. */
  36. @Override
  37. public void onpagestarted (WebView view, String URL, Bitmap favicon) {
  38. LOG.D ("testtimeout", "onpagestarted .....");
  39. //TODO auto-generated method stub
  40. super.onpagestarted (view, URL, favicon);
  41. Timer = new timer ();
  42. TimerTask TT = new TimerTask () {
  43. @Override
  44. public Void Run () {
  45. /* 
  46. * 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
  47. */
  48. if (testjsactivity. This.mWebView.getProgress () < ) {
  49. LOG.D ("Testtimeout", "Timeout ......");
  50. Message msg = new Message ();
  51. Msg.what = 1;
  52. Mhandler.sendmessage (msg);
  53. Timer.cancel ();
  54. Timer.purge ();
  55. }
  56. }
  57. };
  58. Timer.schedule (TT, timeout, 1);
  59. }
  60. /** 
  61. * onpagefinished means page load complete, cancel timer after completion
  62. */
  63. @Override
  64. public void onpagefinished (WebView view, String URL) {
  65. //TODO auto-generated method stub
  66. super.onpagefinished (view, URL);
  67. LOG.D ("Testtimeout", "onpagefinished+++++++++++++++++++++++++");
  68. LOG.D ("Testtimeout", "+++++++++++++++++++++++++"
  69. + testjsactivity.  this.mWebView.getProgress ());
  70. Timer.cancel ();
  71. Timer.purge ();
  72. }
  73. });
  74. 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 ");
  75. }
  76. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.