Posts Tagged components

Android 101 – The basic building blocks of an Android Application


This post talks about the basics and fundamentals of Android development. So if you know java and eager to program for the Android platform, read on.

I recently developed an iOS app for a large organization and published it on itunes. Received some good feedback, however, turns out that half of the users have Android phones. So i started looking at developing an Android version of the iOS app. Plus according to Google, more than 350,000 android devices are being shipped on any given day which translates to 1 million new Android users every 3 days!

Compared to iPhone, Android has quite a few vendors shipping different kinds of Android phones (LG, Samsung, Motorola, HTC, etc.). However, good news is that now there is a one common platform on which to develop Android apps – the latest one is called honeycomb 🙂

So lets start with the basics. And Android app is a collection of several different components viz. Activity, Service, Content Provider, Broadcast Receiver, Intents; all of them are defined in AndroidManifest.xml.

1. Activity – This is how you write an Android user interface. A single activity defines a single screen with a user interface. When you write a user interface for your home screen it will most likely extend the Activity class. It defines simple lifecycle methods like onCreate, onResume, onPause which lets you handle interruptions.

2. Service – it does the majority of processing for your application. Its the logic behind the user interface. E.g. performing a long running process in the background. You can sub class the Service class to write your own custom service.

3. Content Provider – This provides a uniform singular interface to the content & data. The content can be from your app being exposed to the system or other apps or it could be system content e.g. pictures on your phone, contact lists exposed or available to your app via the Content Provider interface. It provides a consistent interface to retrieve/store data via RESTful model supporting CRUD operations.

4. Broadcast Receiver – The application modules communicate with each other using the broadcast receiver. Messages can be initiated by the system or the app. E.g. if a user is using your app and a phone call comes in, the Broadcast Receiver interface will notify your app and your can code to handle that interruption (by possibly delegating to your service). You can opt to receive messages statically via declaring in the manifest file or dynamically by programmatically registering it the code which extends the Broadcast Receiver interface.

5. Intents – Of all the main components we discussed so far, this is the most interesting. Intents can be implicit or explicit. E.g. an explicit intent can be to invoke another screen when a button is pressed on the Activity (UI) in context. An implicit intent is when you create an intent and hand it off to the system to handle it. A good example discussed in one of google I/O boot camp was the barcode example, where the app creates an implicit intent of type SCAN and provide it to another app which handles scanning and returns the resultCode back to the calling application.

Typically you define all the components of your Android app in the manifest file. You can state the features, permissions and sdk version in the manifest. The application tag will have sub tags defining activities, services, receivers and intents.

Having discussed the main components, lets move on now to our first basic task – creating a simple user interface.

In the Android World a UI is a hierarchy of Views and ViewGroups which can be defined either programmatically or in XML layout files. E.g. to create a simple linear layout i can define the following in my xml file (e.g. main.xml under the layout folder of your application resource package):

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout …>

<TextView android:id=”@+id/text”

android:text=”Hello World!” …/>

</LinearLayout>

Though we used linear layout here, its considered best practice to use RelativeLayout to cater for multiple size screens.

In the next post we will use the above discussed concepts to build a simple Android application.

Feedback and Questions are appreciated.

, , , , , ,

Leave a comment