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.

Tuesday, April 20, 2010

Activity in Android

As I had noted in the previous post, Android app can be built of four components and Activity is the most crucial one. Well you can term an activity in Android as a UI  screen. So an application can have more than one activity. All activities are derived from the class Activity.
An Activity is made up of a views. These views are placed in a hierarchy with content View at the root of it.A view in simpler words are controls for android.
To launch an activity, we need to use Intents.An intent is like a message envelope. Its used to pass information or parameters to the activity. The Android API for launching an activity is Context.startActivity(Intent intent) or Activity.startActivityForResult(Intent  intent, int requestCode). The activity being launched, can call its getIntent() method to get the intent information.One activity often starts the next one. startActivityForResult() is called when we expect some return information from the activity, like some choice that the user has made. The result is obtained by calling onActivityResult(int requestCode, int resultCode, Intent data) method of the Activity.

To shut an activity down, you need to call  finish(). An activity started by another one by using startActivityForResult() can be shut by calling finishActivity().

But for an activity, its not just start and stop. There are several states that an activity can go through. It can be active(running and visible on the screen), paused ( visible to user, but in the background) and stopped(not running and currently all resources on the device are taken over by another activity). The following chart explains things more clearly.
activity_lifecycle

So as clear from the above chart, the three states are monitored by the seven functions which are
1. onCreate() - Called when the activity is created
2. onStart() - Called just before the activity is made visible to the user
3. onResume() - Called just before activity starts interacting
4. onPause() - Called when currently running activity needs to be paused and other activity comes into focus.
5. onStop() - When an activity is no longer visible to the user, either because current one is going to be destoyed or another one has gained complete focus.
6. onDestroy() - Called before activity is destroyed.
7. onRestart() - Called just before an activity, previously stopped is going to be started.

This is as far as fundamentals of activity are concerned, but this is by no means complete of Activity. More on this in the next few posts

Thursday, April 15, 2010

The intricacies of Application in Android

The Application is the top layer in the Android Framework. This is the code which we write. As you know, all the application coded in Android are in Java. The Dalvic Virtual Machine in the Andorid Runtime layer is responsible for running this compiled bytecode. This bytecode is packaged into a .apk file which is also called Android Package using the Android Asset Packaging Tool(or aapt in short). This .apk file needs to installed on phones that support the version its packaged for. 

There are certain properties of each android application which is important to know of. 
1. Each process runs as a linux process in Android. Android has the ability to shutdown the process when there is a need to acquire more system resources. So you need to be careful about this while coding your app on Android.
2. Each process runs over its own Dalvik Virtual Machine. So  there is proper isolation of code and data. To conserve system resources we can start two different application(with same Application ID) in the same virtual machine by running them as a single linux process.
3. Each application is assigned its own Linux UserID, so the files from one application is unique to that.You can make two application to have the same Linux UserID, in that case they can share the files.

Architecturally, Android applications can be viewed as made up of four different component:
1. Activity - All UI are termed as an activity
2. Service - Anything that is not UI and does not work in the background is called as service
3. Broadcast Receiver  - It receives various updates and responds to them, eg, low battery, language changed etc.
4. Content Provider - This is the component built to provide data to other applications.

In the next few post I will be digging deep in the different components and how and when to use them.

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.

Preface: Why this blog and why learn Android Programming

Well let me answer the second part of the question first.

Why learn Android Programming?
Answer to this question is pretty simple. Android is a great platform. It bring freedom of thought and expression of creative ideas to the programming for the  mobile world. Going forward, this is going to be one of the most important development platforms for application developers like me.

Why this blog ?
First let me make myself clear, this is not a book that I am writing for others to learn from. This is a means for me to learn Android Programming. I am basically a C++ developer, but I can say that I can program in any language. I know Java basics and so have decided to venture into Android programming. As I learn and understand Android, I would like to make a collection for my reference. Also this blog may serve good for others in whatever ways possible. Also I intend to get myself corrected(by means of reader comments) in case I have an incorrect understanding of something.

So whats the plan?
Plan is simple. I would go about understanding the various features and designs incorporated in the Android Platform. The features of the platform as they mention in order I would go ahead and learn more about are:
  • Application framework
  • Dalvik virtual machine
  • SQLite
  • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
  • Camera, GPS, compass, and accelerometer
  • Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
  • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
  • GSM Telephony (hardware dependent)
I would typically take a topic and dig deep to understand it over a period of week. So I expect myself to write at least a post every week.