秒内没有执行完毕。

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. Visual Studio 2012 未找到与约束 ContractName问题,及printf unsafe问题

    1.用VS 2012 创建c++项目失败,提示未找到与约束 ContractName .............的错误 解决办法:下载VS 2012 补丁,很小的一个补丁,不过很管用 地址:http: ...

  2. xml解析-jaxp之dom解析

    package day06_parser.dom; /** * xml的解析技术:JAXP是Java API for XML Processing的英文字头缩写, * 中文含义是:用于XML文档处理的 ...

  3. luogu P1710 地铁涨价

    嘟嘟嘟 一道最短路好题. 首先明确一点,把一条边的边权变成2,等于删去这条边.因为变成2后最短路肯定不会经过这条边,就相当于删去这条边了. 所以题目变成了依次删去Q条边,求每一次删完边后有几个点的最短 ...

  4. 2018.12.22 Spring学习02

    Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...

  5. 将psd格式批量转换成jpg或png格式(C#自制软件)

    此项目基于.net framework 4.0 将psd格式批量转换成jpg或png格式. 链接:https://pan.baidu.com/s/16IEjX0sHaY9H3Ah7mv6IPQ 提取码 ...

  6. Python-time和datetime模块

    一.time模块 1.表示时间的三种方式 >>> import time >>> time.time() #当前时间戳 1509525556.8485825 > ...

  7. Ubuntu14.04系统下无法连接Wi-Fi无线网以及安装BCM43142网卡驱动的解决方案

    1.问题描述 博主近日开始学习ROS,首先必装Linux操作系统,选择的是Ubuntu14.04,安装过程略过,直接讲问题.安装完系统之后发现一个重要问题,没法使用Wi-Fi,只能使用有线网络,而且网 ...

  8. Android学习笔记_37_ListView批量加载数据和页脚设置

    1.在activity_main.xml布局文件中加入ListView控件: <RelativeLayout xmlns:android="http://schemas.android ...

  9. AngularJS表格排序

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. ajax请求相关问题

    Ajax中async:false/true的作用: async. 默认是 true,即为异步方式,$.ajax执行后,会继续执行ajax后面的脚本,直到服务器端返回数据后,触发$.ajax里的succ ...