`

Multitasking the android way

 
阅读更多

 
 
 
 

28 APRIL 2010

Multitasking the Android Way

 

[This post is by Dianne Hackborn, a Software Engineer who sits very near the exact center of everything Android. — Tim Bray]

Android is fairly unique in the ways it allows multiple applications to run at the same time. Developers coming from a different platform may find the way it operates surprising. Understanding its behavior is important for designing applications that will work well and integrate seamlessly with the rest of the Android platform. This article covers the reasons for Android's multitasking design, its impact on how applications work, and how you can best take advantage of Android's unique features.

DESIGN CONSIDERATIONS

Mobile devices have technical limitations and user experience requirements not present in desktop or web systems. Here are the four key constraints we were working under as we designed Android's multitasking:

  • We did not want to require that users close applications when "done" with them. Such a usage pattern does not work well in a mobile environment, where usage tends to involve repeated brief contact with a wide variety of applications throughout the day.

  • Mobile devices don't have the luxury of swap space, so have fairly hard limits on memory use. Robert Love has a very good article covering the topic.

  • Application switching on a mobile device is extremely critical; we target significantly less than 1 second to launch a new application. This is especially important when the user is switching between a few applications, such as switching to look at a new SMS message while watching a video, and then returning to that video. A noticeable wait in such situations will quickly make users hate you.

  • The available APIs must be sufficient for writing the built-in Google applications, as part of our "all applications are created equal" philosophy. This means background music playback, data syncing, GPS navigation, and application downloading must be implemented with the same APIs that are available to third party developers.

The first two requirements highlight an interesting conflict. We don't want users to worry about closing their apps, but rather make it appear that all of the applications are always running. At the same time, mobile devices have hard limits on memory use, so that a system will degrade or even start failing very quickly as it needs more RAM than is available; a desktop computer, with swap, in contrast will simply start slowing down as it needs to page RAM to its swap space. These competing constraints were a key motivation for Android's design.

WHEN DOES AN APPLICATION "STOP"?

A common misunderstanding about Android multitasking is the difference between a process and an application. In Android these are not tightly coupled entities: applications may seem present to the user without an actual process currently running the app; multiple applications may share processes, or one application may make use of multiple processes depending on its needs; the process(es) of an application may be kept around by Android even when that application is not actively doing something.

The fact that you can see an application's process "running" does not mean the application is running or doing anything. It may simply be there because Android needed it at some point, and has decided that it would be best to keep it around in case it needs it again. Likewise, you may leave an application for a little bit and return to it from where you left off, and during that time Android may have needed to get rid of the process for other things.

A key to how Android handles applications in this way is that processes don't shut down cleanly. When the user leaves an application, its process is kept around in the background, allowing it to continue working (for example downloading web pages) if needed, and come immediately to the foreground if the user returns to it. If a device never runs out of memory, then Android will keep all of these processes around, truly leaving all applications "running" all of the time.

Of course, there is a limited amount of memory, and to accommodate this Android must decide when to get rid of processes that are not needed. This leads to Android's process lifecycle, the rules it uses to decide how important each process is and thus the next one that should be dropped. These rules are based on both how important a process is for the user's current experience, as well as how long it has been since the process was last needed by the user.

Once Android determines that it needs to remove a process, it does this brutally, simply force-killing it. The kernel can then immediately reclaim all resources needed by the process, without relying on that application being well written and responsive to a polite request to exit. Allowing the kernel to immediately reclaim application resources makes it a lot easier to avoid serious out of memory situations.

If a user later returns to an application that's been killed, Android needs a way to re-launch it in the same state as it was last seen, to preserve the "all applications are running all of the time" experience. This is done by keeping track of the parts of the application the user is aware of (the Activities), and re-starting them with information about the last state they were seen in. This last state is generated each time the user leaves that part of the application, not when it is killed, so that the kernel can later freely kill it without depending on the application to respond correctly at that point.

In some ways, Android's process management can be seen as a form of swap space: application processes represent a certain amount of in-use memory; when memory is low, some processes can be killed (swapped out); when those processes are needed again, they can be re-started from their last saved state (swapped in).

EXPLICITLY RUNNING IN THE BACKGROUND

So far, we have a way for applications to implicitly do work in the background, as long as the process doesn't get killed by Android as part of its regular memory management. This is fine for things like loading web pages in the background, but what about features with harder requirements? Background music playback, data synchronization, location tracking, alarm clocks, etc.

 

For these tasks, the application needs a way to tell Android "I would explicitly like to run at this point." There are two main facilities available to applications for this, represented by two kinds of components they can publish in their manifest: broadcast receivers and services.

Broadcast Receivers

A BroadcastReceiver allows an application to run, for a brief amount of time, in the background as a result of something else happening. It can be used in many ways to build higher-level facilities: for example the AlarmManager allows an application to have a broadcast sent at a certain time in the future, and the LocationManager can send a broadcast when it detects interesting changes in location. Because information about the receiver is part of an application's manifest, Android can find and launch the application even if it isn't running; of course if it already has its process available in the background, the broadcast can very efficiently be directly dispatched to it.

When handling a broadcast, the application is given a fixed set of time (currently 10 seconds) in which to do its work. If it doesn't complete in that time, the application is considered to be misbehaving, and its process immediately tossed into the background state to be killed for memory if needed.

Broadcast receivers are great for doing small pieces of work in response to an external stimulus, such as posting a notification to the user after being sent a new GPS location report. They are very lightweight, since the application's process only needs to be around while actively receiving the broadcast. Because they are active for a deterministic amount of time, fairly strong guarantees can be made about not killing their process while running. However they are not appropriate for anything of indeterminate length, such as networking.

Services

A Service allows an application to implement longer-running background operations. There are actually a lot of other functions that services provide, but for the discussion here their fundamental purpose is for an application to say "hey I would like to continue running even while in the background, until I say I am done." An application controls when its service runs by explicitly starting and stopping the service.

While services do provide a rich client-server model, its use is optional. Upon starting an application's services, Android simply instantiates the component in the application's process to provide its context. How it is used after that is up to the application: it can put all of the needed code inside of the service itself without interacting with other parts of the application, make calls on other singleton objects shared with other parts of the app, directly retrieve the Service instance from elsewhere if needed, or run it in another process and do a full-blown RPC protocol if that is desired.

Process management for services is different than broadcast receivers, because an unbounded number of services can ask to be running for an unknown amount of time. There may not be enough RAM to have all of the requesting services run, so as a result no strong guarantees are made about being able to keep them running.

If there is too little RAM, processes hosting services will be immediately killed like background processes are. However, if appropriate, Android will remember that these services wish to remain running, and restart their process at a later time when more RAM is available. For example, if the user goes to a web page that requires large amounts of RAM, Android may kill background service processes like sync until the browser's memory needs go down.

Services can further negotiate this behavior by requesting they be considered "foreground." This places the service in a "please don't kill" state, but requires that it include a notification to the user about it actively running. This is useful for services such as background music playback or car navigation, which the user is actively aware of; when you're playing music and using the browser, you can always see the music-playing glyph in the status bar. Android won't try to kill these services, but as a trade-off, ensures the user knows about them and is able to explicitly stop them when desired.

THE VALUE OF GENERIC COMPONENTS

Android's generic broadcast receiver and service components allow developers to create a wide variety of efficient background operations, including things that were never originally considered. In Android 1.0 they were used to implement nearly all of the background behavior that the built-in and proprietary Google apps provided:

  • Music playback runs in a service to allow it to continue operating after the user leaves the music application.

  • The alarm clock schedules a broadcast receiver with the alarm manager, to go off at the next set alarm time.

  • The calendar application likewise schedules an alarm to display or update its notification at the appropriate time for the next calendar event.

  • Background file download is implemented a service that runs when there are any downloads to process.

  • The e-mail application schedules an alarm to wake up a service at regular intervals that looks for and retrieves any new mail.

  • The Google applications maintain a service to receive push notifications from the network; it in turn sends broadcasts to individual apps when it is told that they need to do things like synchronize contacts.

As the platform has evolved, these same basic components have been used to implement many of the major new developer features:

  • Input methods are implemented by developers as a Service component that Android manages and works with to display as the current IME.

  • Application widgets are broadcast receivers that Android sends broadcasts to when it needs to interact with them. This allows app widgets to be quite lightweight, by not needing their application's process remain running.

  • Accessibility features are implemented as services that Android keeps running while in use and sends appropriate information to about user interactions.

  • Sync adapters introduced in Android 2.0 are services that are run in the background when a particular data sync needs to be performed.

  • Live wallpapers are a service started by Android when selected by the user.

分享到:
评论

相关推荐

    Python库 | multitasking-0.0.10.tar.gz

    标题中的"Python库 | multitasking-0.0.10.tar.gz"指的是一个名为`multitasking`的Python库的版本0.0.10,它被打包成`.tar.gz`格式的压缩文件。这种格式通常用于在Unix-like系统或Python环境中分发源代码包。`.tar....

    Android SDK (SDK Platforms)-android-16.zip

    5. **Multitasking Improvements**:多任务处理能力得到增强,用户可以更方便地在应用之间切换。 总之,Android SDK (SDK Platforms)-android-16.zip包含了一切开发者需要在Android 4.1平台上进行开发的工具和资源...

    multitasking_1.70

    更新时间:2011-08-11资费提示:免费版当前版本:1.70软件语言:中文软件类别:进程管理软件大小:157 KB适用固件:2.1及更高固件内置广告:没有广告适用平台:Android 软件介绍 MultiTaskin Pro可以让您在正在运行...

    Android-API合集

    12. **Multitasking**:Android支持多任务处理,允许用户在多个应用间切换。后台任务可以通过Service或后台Activity进行管理。 13. **Android NDK**:除了Java API,Android还提供了NDK,允许开发者使用C和C++原生...

    multitasking:使用装饰器的非阻塞Python方法

    MultiTasking:使用装饰器的非阻塞Python方法 MultiTasking是一个很小的Python库,可让您简单地通过使用装饰器将Python方法转换为异步,非阻塞方法。 例子 # example.py import multitasking import time import ...

    Android开发的官方demo

    10. **Multitasking and App Lifecycle**:ApiDemos将演示如何处理应用的生命周期方法,如onCreate(), onStart(), onResume(), onPause(), onStop()和onDestroy(),以及如何在多任务环境中正确管理应用状态。...

    iOS.9.For.Users.and.Developers.B0151VT3KA

    Other changes include Keyboard changes, improvements to Search, changes to the way News is handled. Additionally, Updates to Siri, new features on Notes, Multitasking, System Fonts, updates in ...

    MIT JOS Lab 4: Preemptive Multitasking

    《MIT JOS Lab 4:抢占式多任务处理详解》 在计算机科学领域,操作系统是管理计算机硬件资源并提供服务的软件核心。MIT JOS(Journey Operating System)实验是麻省理工学院操作系统课程的一部分,旨在让学生深入...

    Android 9Pie操作手册.rar

    - **Split-screen multitasking** 进一步优化,支持更多应用同时运行,提高工作效率。 5. **连接性与安全** - **Wi-Fi Round-Trip-Time (RTT)**: 支持室内定位,为购物、导航等场景提供更多可能性。 - **...

    android 5.1.1 Api22 sdk源码

    7. **Multitasking & App Lifecycle**:Android 5.1.1支持多任务并行,源码展示了Activity、Service等如何在后台运行和恢复,以及如何有效地管理应用生命周期。 8. **安全与隐私**:源码中包含了加密库、权限验证和...

    android-16

    8. **Multitasking**: 多任务处理能力增强,用户可以更轻松地在多个应用之间切换,同时保持系统的流畅性。 9. **Developer Features**: 对开发者而言,Android-16提供了新的APIs和工具,例如Fragment Transactions...

    英文原版-Samsung Galaxy Tab 10 1 For Dummies 1st Edition

    A vast improvement over the original Galaxy Tab, the 10.1 uses the latest version of the Android operating system and is a 4G LTE mobile device. These upgrades—along with many others, including the ...

    ios应用源码之后台运行(multitasking)以及本地通知(local noti

    本资源“ios应用源码之后台运行(multitasking)以及本地通知(local notifications)”提供了一个完整的背景运行和本地通知实现的示例,包含了图片、书籍资料以及相关的代码,打包名为“backgrounddemo(beta4fixed)”。...

    Keil RTS full

    way with all processors of the 8051 processor family. The sequence control required for simple applications could, of course, be implemented by the user himself. This, however, is not very efficient, ...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    These tasks must cooperate to behave correctly, and threads meet the needs of cooperative multitasking. <br>Chapter 15Annotationsdescribes the annotation types used to document some of the extra-...

    Android API

    14. **Multitasking and Background Processing** Android支持多任务处理,允许应用在后台运行。通过Service和IntentService,开发者可以实现后台任务,如音乐播放、数据同步等。 15. **Android NDK (Native ...

    深入理解ANDROID卷3.pdf

    11. **Multitasking and Multiwindow**:Android支持多任务处理,包括最近应用列表和多窗口模式。开发者需要了解如何在这些场景下设计和调整应用。 12. **App签名与安全**:了解应用的签名过程和安全策略,有助于...

    python multitask-0.2.0.zip

    The task manager temporarily suspends the task (allowing other tasks to run in the meantime) and then restarts it when the blocking operation is complete. Such an approach is suitable for ...

Global site tag (gtag.js) - Google Analytics