Pages

Ads 468x60px

Wednesday, April 28, 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.

More Blogginess

Hello everyone, and welcome to a rare (in this space) blog about blogging. My name is Tim Bray, and I’m the new editor of this Android Developers’ Blog. I am only a student of Android, but I’m a veteran blogger, I’m part of the Android team, and they’ve given me a pretty free hand to find and publish the interesting stories. I’m expecting to enjoy this and hope you will too.

The work on Android is done at various places around the world, but in Mountain View, California there’s a building on the Google campus with an Android statue in front of it, positioned among dessert-themed sculptures that illustrate the major platform releases to date.

As of now, this blog has a header image taken from where some of the Android work happens, behind the statuary looking out. There are a ton of places on the Internet where you can read people’s opinions about what’s happening next with Android, and a lot of them are good. The one you’re reading now is the one that’s written from the inside looking out.

History

This space has been used mostly in a just-the-facts press-release-flavored way and, while that’s been useful, I thought it could be livelier. Because, even after only a few weeks’ exposure to what’s going on here, I’ve discovered that there are a ton of interesting Android stories, and while some of them probably have to be secrets, there are more than enough we can tell to crank up the interest level here.

I offered this opinion internally, loudly and repeatedly, and Android management surprised me by coming back with “OK, it’s your problem now.”

Future

I’m not going to write everything here; I’m going to track down the people who actually do the creative Android-building work and get them to tell their own stories. I will bend over backward to make sure the articles have the voices of the people who write them.

We will go on being a source for hard opinion-free just-the-facts Android news. But I’d like to surround each burst of that with a cluster of reportage about what it means and how we think it will affect the Android communities.

The immediate future is going to be pretty intense, because we’re only a few weeks away from Google I/O, and I don’t think that I’m telling any secrets when I say that there will be Android-related announcements at that event. So the problems that come with the new job probably won’t include scaring up content.

The first new-flavor post is going to be on a currently-hot subject, multitasking and background processing, written by an engineer at the center of the Android universe.

Wish me luck, and stay tuned!

Thursday, April 22, 2010

Mobile Web and Mobile Apps Monetization Strategies














Faced with a prevailing culture where consumers expect web services and digital content to be free, mobile app and mobile web developers need to adopt the right strategy in order to monetize their efforts.

The Freemium model is a classic marketing tool available to mobile app developers to maintain the perception of a free service, while attempting to lock-in customers into some type of charging mechanism. (And I recommend reading Chris Anderson's 'Free: The Future of a Radical Price' book for further insights into this).

But many of the decisions around charging will stem from a look at the value that the mobile consumer perceives he/she is getting from the app in question. Apps with a low perceived value will struggle to charge anything at all, no matter the charging mechanism.

I presented a basic introduction to monetization of mobile apps to the EAE Business School this week. This intro is based on a wider discussion included in the chapter of the same name in my new book on Location Based Services (for early access to the book, click here). In the same chapter, I include a toolkit for deciding how and what to charge for mobile apps and present some success examples of companies that cracked the key to generating recurring revenues.

I took a quick survey of the class (highly educated, international, 20-25 year olds) to see how many had smartphones and what type. About 25% had a smartphone, equally split between iphone and Blackberry. 0% had an Android device (which goes to show that Android still has some way to go in Europe...)

As part of the Q&A, one of the questions that struck me the most was along these lines:

"We hear a lot about mobile apps and the gold rush taking place, but is any app developer actually turning a profit from this activity?"

The answer is, of course, yes, though only those developers with the right combination of an attractive value proposition and the right charging mechanisms are able to succeed in an increasingly crowded marketplace. The potential, with 500+ million apps downloaded to date from the iTunes Store, remains huge. But it pays to have the right monetization strategy in place.

You can see the presentation below:



Tuesday, April 20, 2010

Start-up Funding in times of crisis














As a follow on to the previous post on 'V is for Vulture Capital', here is a short article I submitted to a local business magazine about funding start-ups in times of crisis, based on personal experience in Europe.

"Whether in a boom or bust part of the economic cycle, obtaining funding carries a health warning for innovative start-ups. It can involve an enormous amount of effort for little (or no) return. This is particularly the case when seeking Venture Capital.

The matching of VC funds to start-ups is an imperfect market. The money will not flow to innovative start-ups unless they have the potential of being ‘the next Netscape’ –no matter how world-changing their inventions are.

Plus, there is the funding paradox –it is easier to obtain €20 million of VC money than €1 million. Many VCs will not even look at making investments below €5 million.

In Europe, we still witness a ‘funding gap’ left by Business Angel investment and VC investment. Few investors feel confident enough to sit in between the two and pump in investment amounts of under €1 million. This forces many European start-ups to grow organically and limit international expansion –allowing competitors from outside Europe to catch up all too quickly.

In times of crisis, the funding gap is compounded by the banking sector tightening the screws on their lending procedures. Without the ability to fund working capital using bank overdrafts, loans and credit lines, a larger number of start-ups seek private investment to prop up their balance sheets. As cash is king, these private investors will in turn limit their investments to those start-ups which are themselves cash generating. This compounds the problem and creates a financing vicious circle that is difficult to break unless economic growth resumes."

Thursday, April 8, 2010

Foursquare vs Gowalla -the battle of the check-in at SXSW ( a video comparison)





















AUSTIN- SXSW has come and gone, but it has left an important mark on the social networking scene by making location-aware apps core to the participants' experience of the event.

It also raised the profile of the location 'check-in' feature, as popular Location Based Social Networks like Gowalla and Foursquare competed for the hearts and minds of those present at SXSW.Google's VP of Geo Products, John Hanke, said:

"the check-in had energized the conference."

Who was the winner of the ' mobile check-in' at SXSW, then? Apparently, there wasn't one, though this video produced by SimpleGeo gives you a good feel of how the different services were used (thanks to Troy for bringing this to my attention).

Tuesday, April 6, 2010

HTML5 on iPhone and Offline Web Apps














Making iPhone Web Apps continue to run after they went offline used to be an uphill struggle. However, with the specification of up-and-coming HTML5, you can now load a web app just once and then let it run offline. All this, without a need for a continuous internet connection, giving it the feel of a locally installed native iPhone app.

In his recent post on Mobiforge, Wei-Meng Lee explains how to use Dashcode to write offline iPhone web apps. His article gives you a great step-by-step explanation, from the Configuration of Apache for Web Publishing to Session- and Client-side Storage.

If you are about to develop an app with offline mode, I recommend you read through Wei-Meng's article for further insights. You can find it by clicking here.

Thursday, April 1, 2010

Apple to launch iCar Prototype with total customisation through App Store


















It was only a question of time till Apple decided to venture into new segments by extending its powerful branding franchise. Swiss-watch maker Swatch was the first to foray into the car sector with its successful Smart car model joint venture with Mercedes. Now Apple is looking to replicate this success with its own vision of an 'iCar', combining the latest clean fuel technology with its famous App Store concept.

Full details haven't been revealed, but official sources close to the company state that the iCar will 'integrate Apple's leading iTunes app download technology to provide customisation capabilities never seen before in a car model'.

What this means is that buyers of the iCar will be able to download the entire car specification options one-by-one from a special iCar App Store -from the accelerator display app, to the colour scheme app, rearview camera app etc, effectively 'building' their own electronic display features from scratch. Needless to say, the iCar will come with a WiFi booster, 6-speaker stereo and 9-inch screen and iPad/iPhone dock to also benefit from all the communication and multimedia features loved by Apple fans worldwide.
Related Posts Plugin for WordPress, Blogger...