读书人

关于ANR的官方提议

发布时间: 2012-09-20 09:36:50 作者: rapoo

关于ANR的官方建议

本人博客原文

本翻译整理自: http://developer.android.com/guide/practices/responsiveness.html

参考译文: http://blog.csdn.net/su1216/article/details/7876645

Designing for Responsiveness

IN THIS DOCUMENT
  1. What Triggers ANR?
  2. How to Avoid ANR
  3. Reinforcing Responsiveness
关于ANR的官方提议

Figure 1. An ANR dialog displayed to the user.

It's possible to write code that wins every performance test in the world, but still sends users in a fiery rage when they try to use the application. These are the applications that aren't responsive enough — the ones that feel sluggish, hang or freeze for significant periods, or take too long to process input.

有可能写出的代码赢得了每一次性能测试,但是却仍然使得当用户尝试使用应用时,使他们处于怒火之中。这是因为应用的响应不够迅速 - 用户可能会觉的应用程序运行缓慢,应用程序被挂起,或因冻结很长时间而无反应,或者处理输入时间过长。

In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog, shown at right in Figure 1. The user can choose to let the application continue, but the user won't appreciate having to act on this dialog every time he or she uses your application. It's critical to design responsiveness into your application, so that the system never has cause to display an ANR dialog to the user.

在Android中,系统会监视那些反应不够迅速的应用。如果系统发现某个应用程序反应太慢,它会显示一个叫做Application Not Responding (ANR)的对话框给用户,如Figure 1所示。用户可以选择等待该应用程序让它继续,也可以选择强行结束该应用程序。虽然用户可以选择等待该应用程序让它继续,但是用户其实是非常不喜欢出现该对话框(ANR提示框)

因此设计具有较好响应性的应用程序是非常重要的,这样系统才不会给用户显示一个ANR对话框。

Generally, the system displays an ANR if an application cannot respond to user input. For example, if an application blocks on some I/O operation (frequently a network access), then the main application thread won't be able to process incoming user input events. After a time, the system concludes that the application is frozen, and displays the ANR to give the user the option to kill it.

一般来说,如果一个应用不能快速响应用户输入,则系统显示一个ANR。例如:如果一个应用程序的主线程因为在进行一些I/O操作而被阻塞了(经常是一个网络访问),那么该应用程序的主线程此时就没有能力处理用户的输入事件。一段时间后,系统推断出这个应用冻结了,然后显示一个ANR给用户一个选择杀死此应用或等待该应用程序操作完成后继续。

Similarly, if your application spends too much time building an elaborate in-memory structure, or perhaps computing the next move in a game, the system will conclude that your application has hung. It's always important to make sure these computations are efficient using the techniques above, but even the most efficient code still takes time to run.

相似的,如果你的应用程序的主线程消耗太多时间来建立一个复杂的内存中的结构,或者计算游戏中的下一步,系统会断定你的应用已经挂起。总是保证使用上面的技巧这些计算是有效率的是很重要的,但是即使是最有效的代码也需要时间来运行。

In both of these cases, the recommended approach is to create a child thread and do most of your work there. This keeps the main thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. Since such threading usually is accomplished at the class level, you can think of responsiveness as a class problem. (Compare this with basic performance, which was described above as a method-level concern.)

这两种情况中,推荐的方法是建立一个子线程来做你在那里的大部分工作。这样保持主线程运行并且阻止系统认定你的代码已经冻结。因为这样的线程经常在类级别中完成,你可以把响应性作为一个类的问题考虑。

This document describes how the Android system determines whether an application is not responding and provides guidelines for ensuring that your application stays responsive.

What Triggers ANR?

In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions:

在Android中,应用响应度是由Activity Manager和Window Manager系统服务监视的。Android当发现到下面其中一个条件发生的时候,会特定的应用显示ANR对话框。

读书人网 >移动开发

热点推荐