- 浏览: 662843 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yigeisme:
你好,不已.html和.jsp和.jpg结尾的正则表达式怎么写 ...
深入入门正则表达式(java) - 2 - 基本实例 -
su1216:
多谢指正yigeisme 写道1-12(小时,月份)正则:^( ...
深入入门正则表达式(java) - 2 - 基本实例 -
yigeisme:
1-12(小时,月份)正则:^(1[012]|\d)$应该改成 ...
深入入门正则表达式(java) - 2 - 基本实例 -
heikefangxianceshi:
小三将军 写道楼主,我想问下你给的Real.tar.gz是什么 ...
android手机root后的安全问题 (一) -
heikefangxianceshi:
这个程序运行的前提是手机root,但你的手机怎么root的呢? ...
android手机root后的安全问题 (一)
由于本人英文能力实在有限,不足之初敬请谅解,希望大家落脚同时能指出不足。
本博客只要没有注明“转”,那么均为原创,转贴请注明链接
其他系列的翻译
android activity开发文档翻译 - 1 - 基础篇
android activity开发文档翻译 - 2 - 生命周期篇
android task与back stack 开发文档翻译 - 1
android task与back stack 开发文档翻译 - 2
android task与back stack 开发文档翻译 - 3
本系列并没有对原文100%翻译,可能没有100%的贴出原文
Designing for Responsiveness
为响应度而设计
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)对话框,向下图所示那样。
用户可以选择是让应用继续,但是每一次使用你的应用,用户不会喜欢不得不依照系统弹出的对话框做相应操作
设计较好响应性的应用是很重要的,这样系统才不会给用户显示一个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.
这个文档描述了Android系统如何决定是否一个应用是无响应并且提供直到方针来保证你的应用保持响应。
What Triggers ANR?
什么触发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对话框。
No response to an input event (e.g. key press, screen touch) within 5 seconds
A BroadcastReceiver hasn't finished executing within 10 seconds
一个输入事件5秒内没有反馈
一个BroadcastReceiver在10秒内没有执行完毕。
How to Avoid ANR
如何避免ANR
Given the above definition for ANR, let's examine why this can occur in Android applications and how best to structure your application to avoid ANR.
已知上面ANR的定义,让我们检查为什么这些能在Android应用中发生并且如何最好的架构你的应用来避免ANR。
Android applications normally run entirely on a single (i.e. main) thread.
This means that anything your application is doing in the main thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or Intent broadcast.
Android应用通常整个都运行在一个线程中(主线程)。
这意味着,你应用中做的任何事情都在主线程,花费长时间来完成可能触发ANR对话框,因为你的应用没有给它自己机会来处理用户输出或者Intent广播。
Therefore any method that runs in the main thread should do as little work as possible.
In particular, Activities should do as little as possible to set up in key life-cycle methods such as onCreate() and onResume().
Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a child thread (or in the case of databases operations, via an asynchronous request).
However, this does not mean that your main thread should block while waiting for the child thread to complete — nor should you call Thread.wait() or Thread.sleep().
Instead of blocking while waiting for a child thread to complete, your main thread should provide a Handler for child threads to post back to upon completion.
Designing your application in this way will allow your main thread to remain responsive to input and thus avoid ANR dialogs caused by the 5 second input event timeout.
These same practices should be followed for any other threads that display UI, as they are also subject to the same timeouts.
因此主线程运行的任何方法都应该尽量少的工作。
尤其是,Activity应该尽量少的工作来准备关键的生命周期方法,比如onCreate()和onResume().
潜在的长时间运行的操作,如网络或者数据库操作,或者高代价的计算,比如调整bitmap大小,应该在一个子线程中完成。
然而,这不意味着你的主线程应该在等待子线程完成时而阻塞,你的主线程应该为子线程提供一个Handler,当完成时把结果返回给主线程 - 而不该调用Thread.wait()或Thread.sleep()。
You can use StrictMode to help find potentially long running operations such as network or database operations that you might accidentally be doing your main thread.
你可以使用StrictMode来帮助你找到潜在的长时间运行的操作,比如你也许意外的在你的主线程进行的网络或数据库操作。
The specific constraint on IntentReceiver execution time emphasizes what they were meant to do: small, discrete amounts of work in the background such as saving a setting or registering a Notification.
So as with other methods called in the main thread, applications should avoid potentially long-running operations or calculations in BroadcastReceivers.
But instead of doing intensive tasks via child threads (as the life of a BroadcastReceiver is short), your application should start a Service if a potentially long running action 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 will spawn a new screen that will steal focus from whatever application the user is currently has running.
If your application has something to show the user in response to an Intent broadcast, it should do so using the Notification Manager.
IntentReceiver执行时间上明确的约束,强调了他们那时打算做的:少量的工作 - 在后台大量工作的分离的部分,比如保存一个设置或者注册一个Notification。
所以同在主线程中调用其他方法一样,应用应该在BroadcastReceivers内避免潜在的长时间操作或计算。
但是,如果一个潜在的长时间运行的行为需要接收一个Intent广播的响应,你的应用应该开启一个Service而不是通过子线程做一些高强度的工作。
作为附注,你也应该避免从一个Intent过滤器启动一个activity,因为它将产生一个新屏幕,从用户当前运行的应用中偷走焦点。
如果你的应用有一些内容作为Intent广播的反馈需要显示给用户,它应该使用Notification Manager来完成。
Reinforcing Responsiveness
加强响应
Generally, 100 to 200ms is the threshold beyond which users will perceive lag (or lack of "snappiness," if you will) in an application.
As such, here are some additional tips beyond what you should do to avoid ANR that will help make your application seem responsive to users.
一般来说,在应用中,100到200毫秒是超出用户能不能察觉延迟的临界值。(每个人反应速度不一样,所以会有100ms-200ms这个范围)
这有一些额外的窍门是你为了避免ANR而应该做的,会帮助你的应用对用户来说变得似乎是灵敏的。
If your application is doing work in the background in response to user input, show that progress is being made (ProgressBar and ProgressDialog are useful for this).
For games specifically, do calculations for moves in a child thread.
If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously.
In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen
如果你的应用正在后台工作来响应用户输入,展示那个过程(ProgressBar和ProgressDialog在这点上很有用)
尤其是对游戏来说,在子线程进行对移动的运算。
如果你的应用在初始化阶段较耗时,考虑显示一个版权页或者尽快让出主视图并且异步的填充信息。
不管是哪一种情况,你应该设法表明那个过程,免得用户觉得应用冻结了。
原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/guide/practices/responsiveness.html
转贴请保留以下链接
本人blog地址
发表评论
-
android 第三方app截图
2017-08-17 15:18 1169将View生成相应的图片 ... -
android截图事件监听
2017-07-20 18:37 2007android系统没有对用户截屏行为提供回调的api,所以我 ... -
android widget(桌面小部件)实现
2017-06-30 13:02 1992本文介绍如何自己实现一个widget以及各种注意事项。 ... -
acra简析
2016-07-31 23:54 0版本4.9.1 acra是用来在 ... -
android版本差异
2016-07-28 14:42 0这里只列出部分开发相关内容 notification ... -
android系统提供的常用命令行工具
2016-06-21 13:17 5009本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链 ... -
标示与掩码
2015-10-21 23:00 0掩码(英语:Mask) ... -
通过蓝牙调试(穿戴设备app) - 开发文档翻译
2015-09-30 11:43 2565由于本人英文能力实在 ... -
打包穿戴设备app - 开发文档翻译
2015-09-29 17:19 1786由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没 ... -
弧形进度条(动画版)
2015-07-23 17:00 5839本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接 ... -
使用canvas与Paint在View中居中绘制文字
2015-07-15 23:03 8902本博客只要没有注明 ... -
android 关于提高第三方app的service优先级
2014-06-23 23:14 3108本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链 ... -
android <application> 开发文档翻译
2014-05-24 23:47 5103由于本人英文能力实 ... -
android中各种log的抓取
2013-12-23 13:15 01.logcat的使用 2.内存快照的抓取 ... -
android多语言项目中字符串的移植(bash工具)
2013-11-11 13:31 2437有时候我们会遇到这样一种情况: 一些字符串资源要从原始项 ... -
android 高效显示Bitmap - 开发文档翻译
2013-09-13 17:59 2588由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没 ... -
android 在你的UI中显示Bitmap - 开发文档翻译
2013-09-06 19:33 6562由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没 ... -
android 管理Bitmap内存 - 开发文档翻译
2013-08-27 15:39 4107由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没 ... -
android 缓存Bitmap - 开发文档翻译
2013-08-25 13:01 3143由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有 ... -
android 在UI线程之外处理Bitmap - 开发文档翻译
2013-08-12 13:30 3365由于本人英文能力实 ...
相关推荐
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受开发者欢迎。以下是对Qt技术的详细介绍: 核心特性 跨平台性: Qt设计的核心理念是“一次编写,到处运行”。...
(响应式Web设计) Airbnb JavaScript 代码规范 腾讯alloyteam团队前端代码规范 14天学会安卓开发 Android 官方培训教程 AngularJS最佳实践和风格指南 AngularJS 入门教程 Apache 中文文档2.2 Apache MINA 2 用户...
目录 本篇简介................................................................................8.Android SDK 中Android.net.wifi 简要分析........................................................................
(响应式Web设计) Airbnb JavaScript 代码规范 腾讯alloyteam团队前端代码规范 不可替代的团队领袖培养计划 14天学会安卓开发 Android 官方培训教程 AngularJS最佳实践和风格指南 AngularJS 入门教程 Apache 中文...