RSS

4: How To: Background Multi-tasking Apps

10 Jan

Its such a simple need, you want your app to keep running in the background while the user is sending a text message or playing a game. This behaviour is called background multi-tasking but it can be a big problem for smart-phone software developers

How can running background apps be such a big deal when our desktop/laptop computers run as many apps as we need, and we can switch effortlessly between them? Well there must be something important about multi-tasking otherwise there wouldn’t have been such a long running debate between Android and Apple smart-phone supporters over whose operating system is better at multi-tasking. The truth is that developing apps for smart-phones is very different to developing them for desktop computers because smart-phones currently have very limited computing resources compared to a typical desk-top or lap-top computer. Below is some brief information about developing multi-tasking apps on Android and how HAC makes it easier for the developer.

The Problem of Background Multi-tasking
With modern desktop/laptop computers a user will often have several apps ‘running’ simultaneously and the app developers generally didn’t have to consider what would happen if a user sent their app to the background when switching to another app. The reason desktop developers have an easier time is because the desktop operating system has enough memory(RAM and disk space) to keep all  apps active at once. Contrast the situation on smart-phones which generally have far slower processors and far less memory than typical home computers. As the first smart-phones had such limited computing resources and limited battery life the operating systems often had to  shut down non-essential apps in order to keep the foreground app running. This has traditionally made it much harder for developers to port or write an app for a smartphone because their design must take into account that the app could be shutdown at anytime.

Consider an Android smart-phone and what happens if a user has your app in the foreground but then decides to use another app, perhaps they want to send a text message or play their chess game. Your app is sent to the background and depending upon its design may be deactivated in which case when brought back to the foreground the Android OS will reload it and assume your app has restored its own state.

Android OS distinguishes between Activities and Services. Activities are what the app user interacts with, the screen for instance, and when the app is sent to the background its activity can be deactivated or effectively removed from memory by the operating system. Therefore the app developer must ensure their app’s activity can handle being forcibly removed from memory and restore its previous state when reactivated again. Services by contrast are intended to run continuously and can remain active even when their app’s activity is in the background and has been deactivated.

HAC Apps and Background Multi-tasking
All HAC apps have two components, an Activity and a Service. The activity is the screen(card) that the user will see and interact with, and which the operating system can shut down when the app is pushed to the background. The service is the engine of a HAC app and runs continuously, ready to execute instructions or respond to events, and the operating system will try to keep it running. Usually the engine will be idling and waiting for events to occur such as when the user clicks a button, the GPS system sends new location data or a timer kicks in and request some action. The engine is the core of a HAC app because it holds the apps memory and ensures the app can do whatever the user needs without interruption from other activities such as text messaging, alarm clocks etc.

How to Create your multi-tasking HAC App
When it to comes to creating background apps HAC developers have a much easier time than most other Android software developers because HAC apps can naturally keep running in the background and require no extra coding to get them to multi-task or to restore their state. Developers just have to ensure the app works well in the foreground, and while in the background doesn’t use unnecessary resources. As HAC apps know when they are in the background the developer can switch off screen drawing or get their app to idle if need be. Note, HAC apps can still update their screen while in the background so that when brought to the foreground again their screen is up-to-date. To sum up, developers use the HAC desktop designer to create the user interface(activity) and add their scripts(service) using the Script Editor.

Performance
As HAC apps are able to run continuously in the background it would therefore be helpful to know just how much they affect the Android device’s performance. We carried out a series of tests on an older model of Android phone, a HTC G1, as this would highlight any performance issues due to its relatively low processor speed and memory capacity as compared to newer Android smart-phones.

We did four tests, firstly to see how much CPU and RAM was being used when idling. Then three tests using HAC demo projects that placed an increasing load on the system. In order of increasing load they were, Hello World, GPS Satellite, and Lunar Lander.

Between tests the G1 was switched off to reset it and clear its memory so that all tests were equal. Note, except for the Lunar Lander test, the app screen shots were taken after all the tests were run. Below, as well as the stats, there is also a screen shot of each app as it appears when in the foreground.

To measure performance we used an app called SystemPanel App/Task Manager by NextApp Inc. They also have a lite version on Android MarketPlace. Please note that these test results are only approximate but they do give an indication of how much each app loaded the system.

1 – Idling Performance
The picture below shows the G1 after it had been switched on and was running with no other apps loaded except SystemPanel. The CPU usage is about 5.3% and RAM usage at 27M average.

Android G1 idle

2 – Hello World Performance
The Hello World app uses very few resources and so is a good indicator of how much overhead an empty HAC built Android app has.The pictures below shows the G1 after it had been switched on and only SystemPanel and Hello World were running. The Hello World CPU usage was about 4.6% and effective RAM usage was about 11.1M.

Hello World screen shot

Hello World Overview

Hello Worlds Overview

Hello World Stats

Hello World stats

3 – GPS Satellite Performance
The GPS Satellite app might be expected to use lots of resources when running in the background and actively acquiring GPS satellite data. During the tests it made fix requests every second or so and made about 140 requests in total with most of them being mad when the app was in the background. Note, the app wasn’t sent to the background until satellites were located and fix readings were coming in. The pictures below shows the G1 after it had been switched on and only SystemPanel and GPS Satellite app were running. GPS Satellite app CPU usage was about 10.2% and effective RAM usage was about 12.1M.

GPS screen shot

GPS Overview

GPS Overview

GPS Stats

GPS Stats

4 – Lunar Lander Performance
The Lunar Lander game would be expected to use lots of resources when running in the background and especially use lots of CPU time. The pictures below shows the G1 after it had been switched on and only SystemPanel and Lunar Lander game were running. Two tests were made with the first sending Lunar Lander to the back after a game had finished in which case the CPU usage was about 10.3% and effective RAM usage was about 13.1M. In the second test Lunar Lander was sent to the back after a game had just started in which case the CPU usage was about 19.0% and effective RAM usage was about 14.6M.

The Lunar Lander game

Lunar Lander screen shot

Lunar Lander Overview

Lunar Lander Overview

Lunar Lander Stats Game Over

Lunar Lander Stats

Lunar Lander Game Running

Lunar Lander Stats Game Running

Final Thoughts
Actual numbers are very important for both design and comparison purposes. The tests showed that the CPU usage on an Android G1 smart phone for the Hello World, GPS Satellite, and Lunar Lander game were 4.6%, 10.2% and 19.0% respectively. As the Hello World app effectively sits there doing nothing and uses only 4.6% CPU usage on an old device then most HAC apps while idling should use far less on a modern Android device, that is unless they are very active such as when doing number crunching, drawing graphics, acquiring GPS data etc.

Thankfully for HAC users creating multi-tasking background apps is quite easy and as long as they follow sensible guidelines their apps will place little load on the device. With the newer and more powerful Android smart phones and tablets the ability of devices to run background apps is now no big deal. If you are concerned your users might not like your app running in the background then just tell them your app has the Menu Quit option so they can really quit it.

 
Leave a comment

Posted by on 10/01/2012 in How-to

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: