[This post is by Dianne Hackborn, a Software Engineer who sits really nearby a expect core of everything Android. — Tim Bray]
Android is sincerely singular in a ways it allows mixed applications to run during a same time. Developers entrance from a opposite height might find a approach it operates surprising. Understanding a function is critical for designing applications which will work good as good as confederate seamlessly with a rest of a Android platform. This essay covers a reasons for Android's multitasking design, a impact upon how applications work, as good as how we can most suitable take worth of Android's singular features.
Design considerations
Mobile inclination have technical limitations as good as user experience mandate not benefaction in desktop or web systems. Here have been a 4 pass constraints we were operative underneath as we created Android's multitasking:
We did not wish to require which users close applications when "done" with them. Such a usage pattern does not work good in a mobile environment, where usage tends to involve repeated brief hit with a far-reaching accumulation of applications via a day.
Mobile inclination do not have a oppulance of barter space, so have sincerely tough limits upon mental recall use. Robert Love has a really good essay covering a topic.
Application switching upon a mobile device is intensely critical; we target significantly less than 1 second to launch a brand brand brand brand brand new application. This is generally critical when a user is switching between a couple of applications, such as switching to look during a brand brand brand brand brand new SMS summary whilst watching a video, as good as afterwards returning to which video. A noticeable wait for in such situations will fast have users hate you.
The accessible APIs must be enough for writing a built-in Google applications, as partial of our "all applications have been combined equal" philosophy. This equates to credentials song playback, interpretation syncing, GPS navigation, as good as duplicate downloading must be implemented with a same APIs which have been accessible to third party developers.
The first two mandate highlight an engaging conflict. We do not wish users to be concerned about shutting their apps, though rsther than have it appear which all of a applications have been always running. At a same time, mobile inclination have tough limits upon mental recall use, so which a complement will degrade or even begin unwell really fast as it needs some-more RAM than is available; a desktop computer, with swap, in contrast will simply begin slowing down as it needs to page RAM to a barter space. These competing constraints were a pass motivation for Android's design.
When does an duplicate "stop"?
A common disagreement about Android multitasking is a difference between a routine as good as an application. In Android these have been not tightly joined entities: applications might appear benefaction to a user though an actual routine currently using a app; mixed applications might share processes, or one duplicate might have have have make have use of of of of of mixed processes depending upon a needs; a process(es) of an duplicate might be kept around by Android even when which duplicate is not actively we do something.
The actuality which we can see an application's routine "running" does not meant a duplicate is using or we do anything. It might simply be there because Android indispensable it during a little point, as good as has decided which it would be most suitable to keep it around in box it needs it again. Likewise, we might leave an duplicate for a tiny bit as good as return to it from where we left off, as good as during which time Android might have indispensable to get absolved of a routine for alternative things.
A pass to how Android handles applications in this approach is which processes do not close down cleanly. When a user leaves an application, a routine is kept around in a background, allowing it to continue operative (for e.g. downloading web pages) if needed, as good as come rught away to a foreground if a user earnings to it. If a device never runs out of memory, afterwards Android will keep all of these processes around, indeed leaving all applications "running" all of a time.
Of course, there is a limited volume of memory, as good as to house this Android must confirm when to get absolved of processes which have been not needed. This leads to Android's routine lifecycle, a manners it uses to confirm how critical any routine is as good as to illustrate a subsequent one which should be dropped. These manners have been based upon both how critical a routine is for a user's stream experience, as good as how prolonged it has been given a routine was final indispensable by a user.
Once Android determines which it needs to remove a process, it does this brutally, simply force-killing it. The heart can afterwards rught away retrieve all resources indispensable by a process, though relying upon which duplicate being good created as good as responsive to a respectful ask to exit. Allowing a heart to rught away retrieve duplicate resources makes it a lot simpler to avoid critical out of mental recall situations.
If a user after earnings to an duplicate that's been killed, Android needs a approach to re-launch it in a same state as it was final seen, to safety a "all applications have been using all of a time" experience. This is finished by gripping lane of a tools of a duplicate a user is wakeful of (the Activities), as good as re-starting them with inform about a final state they were seen in. This final state is generated any time a user leaves which partial of a application, not when it is killed, so which a heart can after freely kill it though depending upon a duplicate to respond correctly during which point.
In a little ways, Android's routine management can be seen as a form of barter space: duplicate processes paint a sure volume of in-use memory; when mental recall is low, a little processes can be killed (swapped out); when those processes have been indispensable again, they can be re-started from their final saved state (swapped in).
Explicitly using in a background
So far, we have a approach for applications to implicitly do work in a background, as prolonged as a routine doesn't get killed by Android as partial of a unchanging mental recall management. This is fine for things similar to loading web pages in a background, though what about comforts with harder requirements? Background song playback, interpretation synchronization, place tracking, warning clocks, etc.
For these tasks, a duplicate needs a approach to discuss it Android "I would categorically similar to to run during this point." There have been two main comforts accessible to applications for this, represented by two kinds of components they can tell in their manifest: broadcast receivers as good as services.
Broadcast Receivers
A BroadcastReceiver allows an duplicate to run, for a brief volume of time, in a credentials as a outcome of something else happening. It can be used in many ways to build higher-level facilities: for e.g. a AlarmManager allows an duplicate to have a promote sent during a sure time in a future, as good as a LocationManager can send a promote when it detects engaging changes in location. Because inform about a receiver is partial of an application's manifest, Android can find as good as launch a duplicate even if it isn't running; of march if it already has a routine accessible in a background, a promote can really well be without delay dispatched to it.
When we do a broadcast, a duplicate is given a bound set of time (currently 10 seconds) in which to do a work. If it doesn't complete in which time, a duplicate is deliberate to be misbehaving, as good as a routine rught away tossed into a credentials state to be killed for mental recall if needed.
Broadcast receivers have been good for we do tiny pieces of work in reply to an external stimulus, such as posting a presentation to a user after being sent a brand brand brand brand brand new GPS place report. They have been really lightweight, given a application's routine only needs to be around whilst actively receiving a broadcast. Because they have been active for a deterministic volume of time, sincerely strong guarantees can be done about not killing their routine whilst running. However they have been not suitable for anything of indistinct length, such as networking.
Services
A Service allows an duplicate to exercise longer-running credentials operations. There have been essentially a lot of alternative functions which services provide, though for a contention here their fundamental role is for an duplicate to contend "hey we would similar to to continue using even whilst in a background, until we contend we am done." An duplicate controls when a have use of runs by categorically starting as good as stopping a service.
While services do yield a rich client-server model, a have have make have use of of of of is optional. Upon starting an application's services, Android simply instantiates a member in a application's routine to yield a context. How it is used after which is up to a application: it can put all of a indispensable code inside of a have use of itself though interacting with alternative tools of a application, have calls upon alternative article objects common with alternative tools of a app, without delay retrieve a Service e.g. from elsewhere if needed, or run it in an additional routine as good as do a full-blown RPC protocol if which is desired.
Process management for services is opposite than promote receivers, because an unconditional number of services can ask to be using for an different volume of time. There might not be enough RAM to have all of a requesting services run, so as a outcome no strong guarantees have been done about being means to keep them running.
If there is as well tiny RAM, processes hosting services will be rught away killed similar to credentials processes are. However, if appropriate, Android will remember which these services wish to sojourn running, as good as restart their routine during a after time when some-more RAM is available. For example, if a user goes to a web page which requires vast amounts of RAM, Android might kill credentials have use of processes similar to sync until a browser's mental recall needs go down.
Services can serve negotiate this function by requesting they be deliberate "foreground." This places a have use of in a "please do not kill" state, though requires which it include a presentation to a user about it actively running. This is utilitarian for services such as credentials song playback or automobile navigation, which a user is actively wakeful of; when you're personification song as good as using a browser, we can always see a music-playing glyph in a status bar. Android won't try to kill these services, though as a trade-off, ensures a user knows about them as good as is means to categorically stop them when desired.
The worth of general components
Android's general promote receiver as good as have use of components concede developers to emanate a far-reaching accumulation of fit credentials operations, including things which were never creatively considered. In Android 1.0 they were used to exercise scarcely all of a credentials function which a built-in as good as exclusive Google apps provided:
Music playback runs in a have use of to concede it to continue operating after a user leaves a song application.
The warning clock schedules a promote receiver with a warning manager, to go off during a subsequent set warning time.
The calendar duplicate likewise schedules an warning to arrangement or update a presentation during a suitable time for a subsequent calendar event.
Background record download is implemented a have use of which runs when there have been any downloads to process.
The e-mail duplicate schedules an warning to arise up a have use of during unchanging intervals which looks for as good as retrieves any brand brand brand brand brand new mail.
The Google applications say a have use of to receive push notifications from a network; it in spin sends broadcasts to particular apps when it is told which they need to do things similar to synchronize contacts.
As a height has evolved, these same basic components have been used to exercise many of a major brand brand brand brand brand new developer features:
Input methods have been implemented by developers as a Service member which Android manages as good as functions with to arrangement as a stream IME.
Application widgets have been promote receivers which Android sends broadcasts to when it needs to correlate with them. This allows app widgets to be quite lightweight, by not needing their application's routine sojourn running.
Accessibility comforts have been implemented as services which Android keeps using whilst in have have make have use of of of of as good as sends suitable inform to about user interactions.
Sync adapters introduced in Android 2.0 have been services which have been run in a credentials when a particular interpretation sync needs to be performed.
Live wallpapers have been a have use of started by Android when selected by a user.
Gost Rider
Tidak ada komentar:
Posting Komentar