Hello Android

The year was 2007 the launch of Apples iPhone changed everything, revolutionizing the very niche of smart phones. Phones weren’t phones anymore they were compact mobile computing devices pushed to next level. Soon pretty much everybody started to jump on the smart phone bandwagon, initially following the path paved by Apple & then eventually challenging the very king of the jungle. The biggest challenger & threat to iPhone appeared to be the Android platform.

History of Android

In July 2005 Google acquired a new small start-up company known as Android Inc. A little was known about the company back then apart from that it developed software for mobile phones.

In 2007 Google joined hands with around 70 other industry leaders to form an alliance, which came to be known as Open Handset Alliance to develop a next generation mobile computing platform. The alliance vowed to come together & accelerate innovation in mobile computing to develop a richer, less expensive & open platform, the result of which is just sheer brilliance.

An early look SDK was released in November 2007 & in September 2008 first Android phone, T-Mobiles G1 was launched & subsequently Google release the source code of Android platform under Apache’s open source license.

Today around hundred Android mobile phones exist in the market.

Architecture

Android is an open software platform, which includes a complete software stack from the operating system to the middleware & up through the applications.

At its core Android runs a modified version of Linux kernel specifically the 2.6.* kernel. The diagram below from the Android website shows us how the android platform is implemented. Linux acts as a HAL (hardware abstraction layer) providing memory management, process management, networking etc amongst other things.

The next level up is the native libraries which are basically native C/C++ libraries providing a plethora of functionalities, from SSL implementation, SQLite, Webkit to the surface management. The C library is based on BSD(Berkeley Software Distribution) & is fine tuned for embedded Linux-based devices. The WebKit library is the same library that powers Google Chrome & Apple’s Safari & is used on Android to provide browser support. Font type is responsible for font support. SQLite is responsible for in memory relational management of data. 2D support is present in Skia & 3D graphics support is based on OpenGL ES. Android provides a software implementation of OpenGL ES, which is hardware accelerate-able if the device has a graphics chip.

Adjacent to these libraries lies the Android runtime, which basically consists of Core Android Java libraries sitting on top of Dalvik Virtual Machine.

Rather then using the traditional Java virtual machine Google implemented its own virtual machine the Dalvik VM. Dalvik is the name of the town in Iceland from where the chief programmer of the VM belongs.  Dalvik is highly optimized for the mobile devices with better Garbage collection & lower memory footprint. The Dalvik VM takes the generated java class files & combines them into one or more Dalvik Executable .dex files. Dalvik VM uses a different kind of assembly code generation, in which it uses the registers as the primary source of data storage instead of the stack hence reducing the instructions up to 30 percent on the ARM processors. It should be noted that you cannot directly run the java bytecode generated elsewhere on the Dalvik VM you will have to convert the source code to .dex files to run it on Android.

The Java core Libraries includes all the collection classes, utilities, I/O etc that you have been using in the J2SE implementation of Java apart from Swing & other AWT classes.

Just below the applications lie the Application Framework, it is the toolkit that almost all Android applications use. Activity manager manages the lifecycle of the applications. Package manager keeps track of all applications that have been installed on the device. The window manager manages windows & is basically a wrapper on top of lower level services provided by the surface manager. Telephony manager provides support for telephony related features. Content manager allows for the applications to share their data with other applications. Resource manager handles all the resources related to your app, anything that isn’t code is handled by resource manager. View system consists of UI elements which also handles things like event dispatching layout, drawing etc. Location manager is used to provide geographic related data to the device. For example you can register an intent with the location manager to notify you when you get close to a point. Notification manager allows any application to put notification onto the status bar to alert the user about any interesting event. XMPP allows application to send device-to-device data messages to other android users.

The Android UI

The Android UI framework is in par with the cutting edge UI frameworks available today like JavaFX or Microsoft Silverlight. The entire interface even the animations can be declared in the XML files. Screens or windows in Android are referred to as activities that can consist of multiple views. Views are basic building blocks in Android. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).

Android Application Foundations

Activity

Activity represents a single screen in your application; it contains one or more views.

Intent

Intent is an intention to so some work like for example an applications intent to dial a phone number, to launch an activity or to start a service. Intents are not always initiated by your application, they can used by system to notify your application that a certain event has occurred like for example arrival of a text message or a phone call.

Service

Services in Android are background tasks that can run potentially for longer periods. There are two types of services on Android local & remote. Local services are only accessible to the application that is hosting the service & remote services are meant to be used by other applications running on the device. An example of service could be a music player, which keeps on running in the background.

Content Provider

Enables apps to share data across the apps on the device. Like for example if you want to access the address book on the device you would ask content provider for it.

With all the info under our belt let’s quickly develop a small Hello Android application. Follow the instructions below stepwise

  • Download the eclipse IDE from http://www.eclipse.org/downloads/. Unzip & install it on your system.
  • Download a copy of Android SDK from http://developer.android.com/sdk/index.html. Unzip it & place it in your home directory or where ever you like.
  • Add the location of your SDK directory location to your PATH. On Linux I had to edit ~/.bash.rc by appending this text “export PATH={PATH}:~/android-sdk-linux_86/tools”. Just enter command vi ~/.bashrc, scroll to the end & paste the export statement above by first pressing “i” to enter the insert mode press escape to exit insert mode & then write “:wq” to write ie save & quit the vi editor.
  • Fire up Eclipse go to help menu click on Software Updates add a new site in Available software’s https://dl-ssl.google.com/android/eclipse/
  • Restart Eclipse.
  • Go to window preferences & in Android preferences add the location of your SDK.
  • Use the Android SDK and AVD Manager, included in the SDK starter package, to add one or more Android platforms. Use this page as a reference if you’re unsure http://developer.android.com/sdk/adding-components.html.
  • After that go to File menu -> New and click on the Android project.

  • In the following modal window enter the name of project HelloAndroid.
  • In build target select Android 2.2.
  • In application name texbox write HelloAndroidApp
  • In package name write com.semanticnotion.android or whatever you like
  • Click the create activity checkbox and write HelloActivity in the textbox.
  • Set the minimum SDk version to 5 and click finished.
  • You’ll be presented with a bare minimum of Androids application skeleton. Go to the source folder in Eclipse for the project to edit the HelloActivity.java & modify the onCreate() method as follows:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);tv.setText("Hello Android");

setContentView(tv);

}

  • Also add this import statement on the top

import android.widget.TextView;

  • Then go to Run -> Run Configurations. On the Configurations dialog box insert a new configuration name it RunHelloAndroid.
  • Click browse & select HelloAndroid project.
  • Under launch action select com.semanticnotion.android.HelloActivity from the drop down list.
  • Click Apply & then Run.

And voila!! If everything goes well you’ll see the emulator with your app running on it.

Advertisements