Showing posts with label architecture. Show all posts
Showing posts with label architecture. Show all posts

Saturday, March 26, 2011

Multi Threading models in Android

Android has a UI Thread for updating the UI. For any other call like Disk IO or network activity it is recommended to do it in the in a separate thread. Android provides various alternatives to threads which are nice to have as your activities can be killed anytime leaving your thread hanging. But at the same time you need to be careful to use the right one  for your circumstances.

Following are the options available to you

1. java.lang.Thread

This is the standard java thread and it executes in the VM. For most uses its recommended that you do not use it because of synchronization and updating of UI is difficult via such a mechanism.

2. android.app.Service

A service is a mechanism provide by Android to do various tasks in the background. It runs in the main thread, but its can spawn java.lang.Thread and perform various CPU intensive/IO tasks. Not a recommended approach again because its basically yet another thread spawning mechanism in the end. A service generally has higher priority and is the last to be killed in case of low memory.

3. android.os.AsyncTask

AsyncTask is the preferred way to do things in the background and update the UI based on that. It can be achieved in a very direct manner using this mechanism without the hassles of handlers or thread manipulations or synchronization mechanisms.

4. android.app.IntentService

Android proves a special type of service which is called the IntentService. This implements a "work queue processor" pattern, which means at one time a single task is processed here at a time. The service will stop as soon as  all tasks in its queue are completed. It is especially useful in cases where we are not really interested in the result. Something like writing to the DB asynchronously while you have data in the memory always can be offloaded to an IntentService which will queue the requests and update them.

Since there are various means available, finally its upto the developer to evaluate the various options are weigh in the best mechanism for his scenario.

Do write in the comments, your opinion on this.

Wednesday, April 14, 2010

The Architecture of Android

The architecture of Android consists of the following layers
1. Applications
2. Application Frameworks
3. Libraries
4. Android Runtime
5. Linux Kernel


As made clear from the above diagram, the Applications such as the ones we use on the phone like search application, SMS and email clients, maps etc. form this layer.
These applications make use of core services which are provided by the Application Framework. With things like Location Manager, Notification Manager, various Content Providers etc., the application can depend on them to provide the service.
If we are coding in Java, with eclipse, we make use of Android Runtime which consists of the Dalvik Virtual Machine and the core libraries. This is the part which is responsible for running our applications in transparent way, independent of the machine architecture below.
This layer also acts as a wrapper around the different libraries which include libc, SQLite, Webkit(the browser), 2D and 3D graphics library, Encoder and decoders for various formats. You can make use of the NDK(Native Development Kit) to use these libraries directly.
The bottom of all this is the stable and reliable Linux Kernel. With all driver, memory and power management being handled by this layer.

Whats exactly in each of these layers, I will learn and write about them in the forthcoming posts.