秒内没有执行完毕。

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. 二维数组展示到DataGridView(c#)

    窗体程序中二维数组展示到DataGridView public void TwoDArrayShowINDatagridview(string[,] arr) { DataTable dt = new ...

  2. LeetCodeOJ刷题之12【Integer to Roman】

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  3. Android——HelloWorld

    今天正式加入实验室做安卓,看上去无从下手,让我想到当年学ACM一样,但是也一直搞过来了,现在又是一个新的起点. 废话不多说~~~ Hello World 安装: JDK SDK Eclipse 参考: ...

  4. 【[JSOI2007]文本生成器】

    \(AC\)机上的计数\(dp\)啊 并没有想到反着求出不合法的串的个数,直接正面硬上 设\(dp[i][j][0/1]\)表示匹配出的长度为\(i\),在\(AC\)机上位置为\(j\),没有/有匹 ...

  5. 使用ParseExact方法将字符串转换为日期格式

    实现效果: 知识运用: DateTime结构的ParseExact方法 public static DateTime ParseExact(string s,string format,IFormat ...

  6. 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)

    目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...

  7. prior_box层

    https://www.jianshu.com/p/5195165bbd06 1.step_w.step_h其实就相当于faster中的feat_stride,也就是把这些点从feature map映 ...

  8. 超简单,快速修改Oracle10g的默认8080端口

    因为Oracle数据库默认的端口是8080,这也是tomcat服务器的默认端口. 为了避免端口冲突,我们通常会修改掉其中一个. 这里我们选择修改Oracle数据库的端口. 第一步:以管理员身份运行cm ...

  9. viewport原理和使用和设置移动端自适应的方法(移动适应电脑)

    viewport原理和使用和设置移动端自适应的方法 HTML中: <meta name="viewport" content="width=device-width ...

  10. LeetCode8.字符串转换整数(atoi) JavaScript

    请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...