Pages

Hello geeks !!

Welcome.

This blog gives you several interesting information related to application development for smartphones such as Windows Mobile, iPhone, Android.

You may find pieces of code that might be of help to understand the concept that is being explained.

Please feel free to post back your questions, comments and suggestions.



Tuesday, April 27, 2010

iPhone OS 4.0 - Multitasking

Brief Introduction

The multitasking feature on iPhone OS 4.0, is not 100% multitasking; it is just ‘Task switching’ instead.

Apple provides a method for running those applications which essentially need the ability to run in the background, such as music player, VoIP applications, etc.

This enables users to keep listening to audio, while they do some other operation in the foreground; keep the VoIP application online in order to receive calls, even when some other app is running in the foreground; track location information in the background using location-based apps, while doing other tasks.

Double tapping on the Home button shows a Task bar with the list of applications running the background. Taping on the icon invokes the application from its previous state.
To quit an application, Press and Hold the application icon on the Task Bar.
A ‘-‘ sign appears. Tap on it to quit.

What does the System actually do?



The OS informs the current application when some other application wants to move to the front. This happens when, an incoming notification (call or SMS or other application’s Notifications) pops up, or when the user presses the Home button to move to the home screen.

When there is a notification, the current application temporarily moves to the ‘Inactive’ state until the user responds to the notification. If the user rejects the notification, the application is returned to ‘Active’ state. When the user accepts the notification, the other application is moved to front (Active state) and the current application will be moved to the ‘Background’ state.

If the application does not want to run in the background, then the application is moved from the ‘Background’ to ‘Suspended’ state. A ‘Suspended’ state means that the System freezes the state of the application (UI, objects, etc.) and then kills the app. When the user switches to the application, the application is re-launched from the frozen state.

If the application has requested to run in the background, then the application runs in the background, and it will continue to receive the events (depending upon the implementation). The UI events will be sent to the application only when the application moves to the front. In case of low memory conditions, the application might be killed by the System without being informed.


Implementation

Multitasking on iPhone OS 4.0 can be implemented in different ways depending on the app requirements.


1. Using Push Notifications and Local Notifications.

Push Notifications are the notification messages sent from a remote server to the iphone through the network. The application has to register for the Push Notification service before the application is released to the AppStore.

Local Notifications are the notification messages that are registered within the iphone. Applications can create time-based notifications using the Calendar, to trigger the application launch.


2. Explicitly request for extra time to complete a task.

During runtime, when the app moves to the background, the System informs the application by calling the ‘applicationWillEnterBackground’ method.

The application can request the System for extra time to complete a task, by calling the ‘beginBackgroundTaskWithExpirationHandler’. Calling this method creates a task that can run in the background for a limited amount of time (10 minutes in SDK 4.0 beta 2).

When the time limit is about to expire, the handler is called. The application should complete all the pending tasks and end the task by calling the ‘endBackgroundTask’ by passing the task ID returned when creating the task.
When the handler completes execution the application moves to the Suspended state.

If this method is not called to end the task, then the system terminates the application.

Initiating the background task can be done either when the ‘applicationWillEnterBackground’ method is called, or wrapping up any long running task inside the handler block, so that, whenever the application suddenly moves to the background, the long-running task would be running in the background.

The application can check for the remaining time to expire by using the ‘backgroundTimeRemaining’ property, and complete the task accordingly.

This method is useful for applications when a file download is pending, etc.
In this case, the file continues to download in the background.


3. Specifying the ‘UIBackgroundModes’ property in the Info.plist file.

Some applications like music players, VoIP, etc. will surely need to run in the background. Hence Apple enables such apps to declare itself to be able to run in the background by specifying the property as ‘voip’ or ‘audio’ or ‘location’.


VoIP

Most VoIP apps require being able to play audio in the background, hence, it requires both ‘voip’ and ‘audio’.


Configure Sockets for VoIP

When the application gets suspended, the sockets need to be still alive to continue receiving data. When the app is suspended, the app makes a transparent hand-off of the socket to the System. Whenever data is received in the socket, the System wakes the application.

In case the application exits unexpectedly, the System re-launches the application, since VoIP applications need to keep running.

After creating the Input and Output streams (Read and Write streams), specify the ‘NetworkServiceType’ property of the streams to VoIP.


Set a Keep-Alive Handler

When the application moves to the background, the application needs to specify a time interval which is the frequency in which the System wakes the application in order to check the network connections.

This can be done by calling the ‘setKeepAliveTimeout:handler:’ method. The minimum timeout value should be 300 seconds. When the timeout is about to expire, the handler is called, which performs the task of checking the connection.



Background audio

Set the UIBackgroundModes property value to ‘audio’. This enables the application to keep running in the background until audio is being played. The application does not go to the ‘Suspended’ state; hence the audio callbacks operate normally.

An audio streaming application would download data from its server, and push audio samples for playback.



Background Location

If the application requires precise location information at regular intervals, then, set the UIBackgroundModes property value to ‘location’. Whenever a location event arrives, ‘locationManager:didUpdateToLocation:fromLocation’ method is called. Navigation applications would use this method to continuously receive location information. This method increases power usage, and hence not recommended.

If the application requires location information only when there is a significant change, then the UIBackgroundModes property need not be set.

The application can call ‘startMonitoringSignificantLocationChanges’ method of CLLocationManager. This will send notifications only when there is a significant change in the location. This method will be called even if the application is suspended or not running at all. This method is the recommended one, since it does not drain the battery.



Efficiency in Multitasking

1. The UI is not updated when the application is in the background or suspended state. Hence, update the views only when the application is in the foreground.

2. Whenever the application moves to the background, save the state. Even though the System freezes the state and resumes, the application needs to manage application specific states.

3. Release unnecessary memory while moving to the background, since, on low memory conditions, the application might be killed.

4. Perform only the high priority tasks in the background, since the execution time given to the background applications is limited.

5. Do not use shared system resources in the background, such as AddressBook, etc. Priority to shared resources is always given to the foreground application, and other background applications holding to these resources will be terminated.

6. Perform network related tasks in the background, but before being suspended, cancel such tasks.

1 comment:

  1. We're serious about increasing your sales. custom applications
    ipad application development

    ReplyDelete