秒内没有执行完毕。

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屏或者尽快让主视图快速显示处理,然后才显示其他的视图。不管是哪一种情况,你应该设法表明程序正在往前执行,以免用户觉得应用冻结了,造成不好的用户体验。

Android开发——Google关于Application Not Responding的建议的更多相关文章

  1. Android开发-API指南-<application>

    <application> 英文原文:http://developer.android.com/guide/topics/manifest/application-element.html ...

  2. android开发之重写Application类

    在android应用开发中,重写Application也算是比较常见的,以前开发的一些程序太过于简单,都不要重写这个类,但是在真正的商业开发中,重写Application类几乎是必做的. 为什么要重写 ...

  3. Google Map和桌面组件 Android开发教程

    本文节选于机械工业出版社推出的<Android应用开发揭秘>一 书,作者为杨丰盛.本书内容全面,详细讲解了Android框架.Android组件.用户界面开发.游戏开发.数据存储.多媒体开 ...

  4. Android系统Google Maps开发实例浅析

    Google Map(谷歌地图)是Google公司提供的电子地图服务.包括了三种视图:矢量地图.卫星图片.地形地图.对于Android系统来说,可以利用Google提供的地图服务来开发自己的一些应用. ...

  5. Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...

  6. Android Studio重构之路,我们重新来了解一下Google官方的Android开发工具

    Android Studio重构之路,我们重新来了解一下Google官方的Android开发工具 记得我的第一篇博客就是写Android Studio,但是现在看来还是有些粗糙了,所有重构了一下思路, ...

  7. 跟Google学习Android开发-起始篇-构建你的第一个应用程序(4)

    说明:此系列教程翻译自Google Android开发者官网的Training教程,利用Chome浏览器的自动翻译功能作初译,然后在一些语句不顺或容易造成误解的地方作局部修正.方便英文不好的开发者查看 ...

  8. Google主推-Android开发利器——Android Studio,这可能是最全的AS教程!

    Android Studio使用手册 "工欲善其事必先利其器" 作为一个Android开发人员来说,一款好的开发工具也是相当重要的,在相当长的时间礼,Google都是基于Eclip ...

  9. 操作系统:Android(Google公司开发的操作系统)

    ylbtech-操作系统:Android(Google公司开发的操作系统) Android是一种基于Linux的自由及开放源代码的操作系统.主要使用于移动设备,如智能手机和平板电脑,由Google(谷 ...

随机推荐

  1. Tomcat与MySQL的数据源连接方法

    Tomcat配置数据源,由于项目经常访问数据库,需要不断地打开关闭,这就耗费了大量的资源.所以用数据源的方式访问数据库. 大体步骤: 配置server.xml 配置项目所在的WebRoot/WEB-I ...

  2. Android 高级UI设计笔记09:Android实现无限滚动列表

    1. 无限滚动列表应用场景: ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们 ...

  3. 基于Qt的相似QQ好友列表抽屉效果的实现

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167     前段时间在忙毕业设计, ...

  4. python:pymysql模块使用

    一,基本使用 # 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,p ...

  5. 小练习——关于循环条件---for

    1.打印100个“非常”的句子 static void Main(string[] args) { //打印20个“非常" Console.WriteLine("打印100个非常& ...

  6. 清除IE地址栏中的历史网址

    实现效果: 知识运用: RegistryKey类的GetValueNames和DeleteValue方法 实现代码: private void button1_Click(object sender, ...

  7. markdown的常用高级操作。

    符号 说明 对应编码(使用时去掉空格) 英文怎么说 & AND 符号 & amp; ampersand < 小于 & lt; little 大于 & gt; gr ...

  8. JavaEE权限管理系统的搭建(七)--------管理用户的增删改

    本小结讲解管理用户的增删改查实现, 首先是添加用户,如下图所示,可以看到添加用户的同时也要给用户分配角色,至少给用户分配一个或者是多个角色 页面js部分: $.ajax({ //几个参数需要注意一下 ...

  9. js邮箱验证,身份证验证,正则表达式

    邮箱验证: html部分: 邮箱验证:<input type="text" id="mail" value="" / onkeyup= ...

  10. Struts2 第三讲 -- Struts2的处理流程

    4.Struts2的处理流程 以下是struts-defautl.xml中的拦截器 建议通过这个struts-default的副本查看,更形象 它实现了很多的功能,其中包括国际化,文件上传,类型转换, ...