秒内没有执行完毕。

2.       避免ANR的一些建议

Android applications normally run entirely on asingle (i.e. main) thread. This means that anything your application is doingin the main thread that takes a long time to complete can trigger the ANRdialog because your application is not giving itself a chance to handle theinput event or Intent broadcast.

Android应用通常整个都运行在一个主线程中。这意味着,你的应用中做的任何事情都在主线程,如果耗时太多就可能触发ANR对话框,因为主线程来不及处理用户输出或者Intent广播及其他回调函数。

Therefore any method that runs in the mainthread should do as little work as possible. In particular, Activities shoulddo as little as possible to set up in key life-cycle methods such asonCreate()and onResume(). Potentially long running operations such as network or databaseoperations, or computationally expensive calculations such as resizing bitmapsshould be done in a child thread (or in the case of databases operations, viaan asynchronous request). However, this does not mean that your main threadshould block while waiting for the child thread to complete — nor should youcall Thread.wait() or Thread.sleep().

因此任何在主线程中调用的函数都应该做尽量少的工作。尤其是四大组件的生命周期函数,比如Activity的生命周期函数 onCreate() 和onResume()。对于需要长时间运行的操作,如网络或者数据库操作,或者高代价的计算,或者调整bitmap大小,应该放在一个子线程中来执行。然而,这不意味着你的主线程应该在等待子线程完成时而阻塞,你的主线程应该为子线程提供一个 Handler,当完成时把结果返回给主线程,而不该调用 Thread.wait() 或 Thread.sleep()。

You can use StrictMode to help find potentiallylong running operations such as network or database operations that you mightaccidentally be doing your main thread.

你可以使用StrictMode 来帮助你在主线程中查找潜在的耗时操作,比如对网络或数据库操作。

Applications should avoid potentiallylong-running operations or calculations in BroadcastReceivers. But instead ofdoing intensive tasks via child threads (as the life of a BroadcastReceiver isshort), your application should start a Service if a potentially long runningaction needs to be taken in response to an Intent broadcast. As a side note,you should also avoid starting an Activity from an Intent Receiver, as it willspawn a new screen that will steal focus from whatever application the user iscurrently has running. If your application has something to show the user inresponse to an Intent broadcast, it should do so using the NotificationManager.

在BroadcastReceivers内应该避免的长时间操作或计算。对于耗时的工作,由于BroadcastReceivers的生命周期是非常短暂的,因此你也不应该在其中开启一个子线程来做该工作,而是应该启动一个 Service,再在这个Service中创建一个子线程来做该工作。另外,你也应该避免从一个Intent过滤器启动一个activity,因为它将产生一个新屏幕,从用户当前运行的应用中偷走焦点。如果你的应用中有一些内容作为Intent广播的反馈需要显示给用户,它应该使用Notification Manager来完成。

If your application is doing work in thebackground in response to user input, show that progress is being made(ProgressBar and ProgressDialog are useful for this).

如果你的应用正在后台工作来响应用户输入,那么给用户显示你的执行进度是个不错的选择(ProgressBar和ProgressDialog在这点上很有用)。

If your application has a time-consuminginitial setup phase, consider showing a splash screen or rendering the mainview as quickly as possible and filling in the information asynchronously. Ineither case, you should indicate somehow that progress is being made, lest theuser perceive that the application is frozen.

如果你的应用在初始化阶段较耗时,考虑显示一个splash屏或者尽快让主视图快速显示处理,然后才显示其他的视图。不管是哪一种情况,你应该设法表明程序正在往前执行,以免用户觉得应用冻结了,造成不好的用户体验。

最新文章

  1. 如何利用rem在移动端不同设备上让字体自适应大小
  2. iOS学习之MVC,MVVM,MVP模式优缺点
  3. app与后台通信协议
  4. 十 SSH
  5. 迁移到 Express 4.x
  6. [反汇编练习] 160个CrackMe之011
  7. ExtJS4.2学习(12)基于表格的右键菜单(转)
  8. 用JavaScript获取页面上被选中的文字的技巧
  9. Linux内核中的通用双向循环链表
  10. brew 更换国内源(镜像)
  11. js 数组排除重复值(string)
  12. 【BZOJ 1579】 1579: [Usaco2009 Feb]Revamping Trails 道路升级 (最短路)
  13. static和final修饰方法
  14. C#连接MySQL
  15. .NET Core跨平台部署
  16. 湘潭邀请赛+蓝桥国赛总结暨ACM退役总结
  17. WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)
  18. 关于django1.7.7使用ajax后出现“CSRF token missing or incorrect”问题的解决办法
  19. 地球椭球体(Ellipsoid)、大地基准面(Datum)及地图投影(Projection)三者的基本概念
  20. 很幽默的讲解六种Socket IO模型

热门文章

  1. 关于selenium无法定位动态元素的解决方法
  2. 解决Could not get lock /var/cache/apt/archives/lock
  3. JQuery前端技术记录
  4. react中虚拟dom的diff算法
  5. PHP设计模式——责任链模式
  6. Xshell的使用
  7. 史上最简单的 SpringCloud 教程 | 第十四篇: 服务注册(consul)
  8. Python 学习笔记(四)数字(二)
  9. CentOS 7 安装oracle 11.2.0.4 Error in invoking target 'agent nmhs' of makefile
  10. Autofac4.0以上的版本通过json配置文件方式实现IOC的MVC5设置