Google's Android operating system for mobile phones has already created a
buzz in the market even before any manufacturer started shipping mobile phones
based on Android. Though key players of mobile operating systems like Symbian,
Microsoft, and Apple are already established in this field, Google has tried to
make its presence felt by giving many firsts with Android to the developers. The
main reason for the buzz being it's the first truly open source comprehensive
platform for mobile phones. The Android includes an operating system, user
interface, and applications developed using the Android SDK. In all, the Android
is a complete set of software that is needed to run a mobile phone without
having any proprietary impediment from the manufacturer, which till yet used to
hinder mobile innovations.
Android will provide an open mobile software platform that will enable
creation of applications that would run and give same user experience on any
Android-based phone. This hasn't been the case till now because developers had
to refer to the manufacturers' phones specifications before developing the
mobile applications for it. So with Android, if the developer has an idea of a
killer app for mobiles he won't have to go into mobile phone's specifications
but rather develop the app and have it deployed on any Android-based handset
irrespective of its manufacturer. In the last month's issue we had talked about
the Open Handset Alliance (OHA) and also had shown how to get started with the
Android application development. Now, let's see what's inside Android and what
tools and APIs do Android provide for innovative app development, and later we
will take the Android app development to next step.
Direct Hit! |
Applies To: Java developers USP: Platform for developing rich mobile apps Primary Link: http://code.google.com/android/download.html Keywords: Google Android On CD: NA |
Anatomy of Android
The Android operating system includes four major components: the kernel,
core libraries, runtime, and application framework. The Android SDK includes a
Linux 2.6 kernel as the operating system for the mobile phone that provides the
core system services such as memory management, process management, security,
networking, drivers, etc. This kernel also provides the base for abstraction
layer between the hardware and the rest of the software stack. On top of this
hardware abstraction layer rests a set of native C/C++ libraries for features
such as media codecs, SQLite database, and WebKit. The WebKit is an open source
Web browser upon which the Android's integrated Web-browser is based. The next
layer in the Android software stack is of the Android's runtime. Google has
specifically chosen Dalvik Java virtual machine (VM) for Android as it is
fine-tuned for mobile devices in terms of limited battery and CPU conditions.
The Dalvik Java VM has a compressed bytecode format and can simultaneously run
multiple applications with lower resource consumption like memory. Therefore
each Android application runs in its own process, with its own instance of
Dalvik VM and is in Dalvik executable (.dex) format. This results in lesser
memory consumption as .dex format files have minimal memory footprint. For
functionalities like threading and low-level memory management Dalvik VM relies
on the underlying Linux kernel.
Android's architecture consists of different components with the top-level apps being able to access the Application Framework's core functional libraries or APIs |
The application framework layer provides developers with access to APIs that
are being used by the core applications. This allows developers to build
applications that can access phone functionalities like making calls, accessing
calendar, or even using the camera of the phone. The Android SDK uses Java for
the application development, but it does not incorporate the complete JDK. This
has resulted in Android having core Java packages like java.nio, java.lang, and
java.util. But the traditional Java user interface packages like Swing and AWT
are absent. This user interface support has now been switched with custom 2D
graphics library and 3D graphics library, based on OpenGL. These new graphics
library provide better graphics experience to the users and can also easily be
used and learned by developers.
Though, Android will ship with Java-based core applications including email
client, SMS program, calendar, maps, and contacts among others, it would provide
an opportunity for developers to build upon these applications and customize
them to make new innovative apps.
Maps and Location APIs
With Android SDK, Google provides two APIs: Location and Maps. As Google has
the required knowhow of the server infrastructure to provide location-based
service through Google Earth and Google Maps, it seems an intelligent move from
Google to provide two useful packages: com.google.android.maps and
android.location, along with Android SDK. The android.location package contains
several classes related to location services in the Android platform. This
package introduces the LocationManager component of the core application
framework which provides an API to determine location information if the
underlying device provides the support. While the com.google.android.maps
package incorporates classes related to rendering, overlaying, and controlling
the information on Google Maps.
Thus with LocationManager, a developer can write an application that would
alert the user when he comes near a certain location and using the Maps APIs the
user can also be shown the route to that location using Google Maps.
A custom made application showing through Location APIs how to locate friends in the vicinity |
Though these APIs for extracting location information based on the nearby
cellular network towers isn't new, it was already present with Jaiku. Running
Jaiku on Symbian S60 platform mobile phones, one could get the location of the
device through the GPS data. Now as Google has bought the Jaiku, it can provide
location specific advertising to the users. Also apps can be build by developers
to provide location-based information, like an application that may tell you how
far you are from another friend and also an intuitive map can be displayed to
user on his Android-based phone showing the road route to his friend's location.
Android application
In the last issue we have seen how to install and start a new project to
build Android apps using Eclipse. An application built on Android will be
consisting of four building blocks-Activity, Intent, Intent receiver, and
Content Provider.
Activity: Activity is the most common of the four building blocks of Android
application. An activity is screen of the application and each activity is
implemented as a single class and it extends the Activity base class. Each of
these activity classes represents the user interface composed of views and
interacts with him and responds to events.
Intent: Intent describes what an application wants to be done. Intents are
special classes that Android uses to move from one screen to another.
IntentFilter is a related class for Intents, while an Intent is effectively a
request for a task to be done, IntentFilters are description of what Intents an
activity can handle.
With the built-in Maps app one can view Google Maps through the emulator. Through Location and Maps APIs, developers can overlay maps to customize them |
IntentReceiver: IntentReceiver is used in application code to execute actions
based on certain external events or requests. For example, IntentReceiver can be
used to execute an activity when the phone rings. For an IntentReceiver to be
called it's not important that the application has to run; the system will start
the app whenever the IntentReceiver is triggered.
ContentProvider: ContentProvider is a class that implements set of methods to
allow other applications to store and receive the type of data that is handled
by the content providers like SQLite database store, etc.
Now we have developed an application where Activity was involved, let's move
ahead and create an application that's based on Intent.
Favorite Page App
We will make an application that will list user's favorite Web pages and
when user clicks on one from the list, the browser is triggered and the website
corresponding to that is opened, as the page listing the favorite links would be
the Activity that will send Intent to the system to open a Web-browser.
Start a new Android project in Eclipse and name it as BookmarkList. On new
android project window screen, give the properties values as:
- Package Name: com.pcq.android
- Activity Name: BookmarkList
- App Name: Favorites Page List
When the project opens in Eclipse, BookmarkList.java file opens. The class
BookmarkList would be having the onCreate() method, we will have to override
this method as shown in the snippet below:
public class BookmarkList extends ListActivity {
public void onCreate(Bundle params) {
super.onCreate(params);
String<> siteList = new String<>{"GMail", "PCQuest", "Google", "Yahoo", "HotMail",
"IndiaTimes"};
this.setListAdapter(new ArrayAdapter
android.R.layout.site_list, siteList));
}
}
Through sitelist attribute we will create a list of favorite pages of the
user. As we intend to open a new browser Window when the user clicks a
list-item, we are extending our class to ListActivity rather than an Activity.
With ListActivity when the application runs we will see the favorite pages
appearing as a list to the user. In the above code snippet we are creating the
list during runtime and not predefining the list through the layout descriptor's
XML file. What we are doing here is using an ArrayAdapter creating the list
using the sitelist's array values. To capture the click of the user and to do
coding for the action to be performed we will have to override the
onListItemClick() method. When the user clicks on a list-item, we have to create
an Intent that will trigger the launch of the Web-browser. In the following code
snippet, we are defining the onListItemClick() method for the BookmarkList class
and in that we are also defining the Intent that has to be created for
instantiating the launch of the Web-browser.
The Favorite Pages List app shows the list of user's favorite pages. When user clicks on PCQuest, the browser is instantiated and the corresponding website opens |
protected void onListItemClick(ListView l, View v, int
position, long id){
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String siteName = o.toString();
Intent webIntent = null;
try {
if(siteName.toLowerCase().equals("google")) {
webIntent = new Intent("android.intent.action.VIEW",
new ContentURI("http://www.google.com"));
}
else if(siteName.toLowerCase().equals("pcquest")){
webIntent = new Intent("android.intent.action.VIEW",
new ContentURI("https://www.pcquest.com"));
}
else{
webIntent = new Intent("android.intent.action.VIEW",
new ContentURI("http://www.gmail.com"));
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(webIntent);
}
Through the position parameter we can get what list-item user had clicked and
corresponding to that we can get the content of the list-item as string in
attribute siteName. Now we have to define the Intent where corresponding to the
siteName string attribute we will trigger the Web browser to open that
particular website. As we had discussed earlier also that an Intent describes
what an application wants to be performed, an Intent provides a facility for
performing runtime binding between the code of different activities in the
application. It can be thought of as acting as glue between different
activities. Here BookmarkList activity initiates another activity of opening of
a browser and webIntent is the Intent that defines what action has to be
performed for the following up activity of browser opening.
So we define in webIntent that a new view has to be created having the
content from the specified URL. By siteName attribute we are comparing what
list-item was clicked by user and the corresponding website is opened through
the webIntent. The android.intent.action.VIEW will launch the new view
implicitly at runtime from within the application as earlier we used to use
AndroidManifest.xml file to describe the views explicitly.
When we run the application, we can view in Android's emulator the Favorite
List Page application running and when the user selects a list-item from the
list a browser gets launched showing the corresponding website.
Conclusion
Though Android-based phones are not available yet, but through the
elaborative and intuitive emulator developers can develop applications and have
the look, feel, and functioning of their applications. With Android, Google has
forayed into mobile platform and it's time that will tell how successful Android
will be in delivering the much-talked excellent user experiences when compared
to Apple's iPhones.