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. * 创建一个WebViewClient,重写onPageStarted和onPageFinished
  31. *
  32. *
  33. * onPageStarted中启动一个计时器,到达设置时间后利用handle发送消息给activity执行超时后的动作.
  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. * 超时后,首先判断页面加载进度,超时并且进度小于100,就执行超时后的动作
  47. */
  48. if (TestJsActivity.this.mWebView.getProgress() < 100) {
  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指页面加载完成,完成后取消计时器
  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. }

其中要注意的是onPageFinished的两点,第一点,官方是这样解释的:

Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).

也就是说,程序只认为DOM加载完成就完成了,正在加载的图片不在这个范围之内。

第二点是,如果在JS文件中又动态去加载另一JS,onPageFinished方法会在所有的JS全部加载完毕后才调用。

webview--网络超时的更多相关文章

  1. NSURLConnection 网络超时的那些事(转别人整理的)

    NSURLConnection 网络超时的那些事(转别人整理的) 在ios平台上做网络开发最常用的两个类: NSMutableURLRequest *urlRequest = [[NSMutableU ...

  2. Charles模拟网络请求页面的网络超时测试

    正常情况下网络连接超时可能的原因有以下几点: 1.网络断开,手动的关掉了网络的连接 2.网络阻塞,导致你不能在程序默认等待时间内得到回复数据包. 3.网络不稳定,网络无法完整传送服务器信息. 4.系统 ...

  3. pip安装拓展包--网络超时/Read timed out问题

    pip安装拓展包--网络超时/Read timed out问题 解决方案:切换镜像源(墙皮太厚) 在后面加上: -i https://pypi.douban.com/simple example: p ...

  4. (转)HttpURLConnection中设置网络超时

    转:http://www.xd-tech.com.cn/blog/article.asp?id=37 Java中可以使用HttpURLConnection来请求WEB资源.HttpURLConnect ...

  5. 关于AFNetworking访问网络超时的设置

    前言:有的猿会发现在设置AFNetworking访问网络超时时,直接用self.manager.requestSerializer.timeoutInterval =10.f不起作用. 解决办法:经过 ...

  6. android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理

    Android应用程序访问互联网资源时,在Wifi的情况下处理网络连接按照上文所讲述的方法步骤即可顺利实现:但如果当前Android设备的联网方式是通过移动运营商的网络服务为中转,间接访问的互联网资源 ...

  7. WebView(网络视图)的两种使用方式

    WebView(网络视图)能加载显示网页,可以将其视为一个浏览器.它使用了WebKit渲染引擎加载显示网页,实现WebView有以下两种不同的方法:第一种方法的步骤:1.在要Activity中实例化W ...

  8. 当pip安装因为网络超时而无法安装的时候慢

    2.4 尝试pip --default-timeout=1000 install  https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp ...

  9. 安卓奇葩问题之:设置webView超时

    我只想说:what a fucking day! 今天要做一个webView的超时功能,于是开始百度,一看貌似很简单啊,于是开始copy了下面的代码. import java.util.Timer; ...

  10. 4.2.1 网络请求之HTTP

    HTTP请求&响应:(常用的只有Post与Get,还有Head/put/delete/connect/options/trace) Get&Post(建议用post规范参数传递方式,并 ...

随机推荐

  1. Linux驱动设计—— 中断与时钟@request_irq参数详解

    request_irq函数定义 /*include <linux/interrupt.h>*/ int request_irq(unsigned int irq, irq_handler_ ...

  2. Make和Makefile编写(详见GCC手册)

    Makefile和Make Rules 多模块软件.依赖树和Make 默认规则 Make使用程序对简单变量的支持 内建变量 虚目标 特殊目标 一般性语法错误及其纠正措施 命令行的使用和调试 Makef ...

  3. Linux驱动设计——字符设备驱动(一)

    Linux字符设别驱动结构 cdev结构体 struct cdev { struct kobject kobj; struct module *owner; const struct file_ope ...

  4. springMvc源码学习之:利用springMVC随时随地获取HttpServletRequest等对象

    一.准备工作: 在web.xml中添加 <listener> <listener-class> org.springframework.web.context.request. ...

  5. PosPal银豹收银系统

    http://pospal.cn/pc.html 注册公司的地址,便宜的快法务 http://www.kuaifawu.com/

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. log tree(merge)

    http://www-users.cs.umn.edu/~he/diff/p256-severance.pdf http://www.eecs.harvard.edu/~margo/cs165/pap ...

  8. 谓词的使用 -ios

    #import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying> @property(nona ...

  9. Python Queue实现生产与消费

    Python Queue模块详解 from:https://blog.linuxeye.com/334.html Python中,队列是线程间最常用的交换数据的形式.Queue模块是提供队列操作的模块 ...

  10. 【转】POP3、SMTP和IMAP之间的区别和联系

    POP3 POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议.它是因特网电子邮件的第 ...