Mobile Application
Development Using Android
Unit I
WHAT IS ANDROID?
• Android is a mobile operating system that is based on a modified version of Linux.
• It was originally developed by a startup of the same name, Android, Inc.
• In 2005, as part of its strategy to enter the mobile space, Google purchased Android and took over its development work.
Android Versions
Features of Android
• Storage — Uses SQLite, a lightweight relational database, for data storage.
• Connectivity Messaging — Supports both SMS and MMS.
• Web browser — Based on the open source WebKit.
• Media support.
• Hardware support — Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor, and GPS.
• Multi-touch
• Multi-tasking
• Flash support
• Tethering
Android Devices in the Market
– Smartphones
– Tablets
– E-reader devices (like Amazon’s kindle and Noble’s NOOK color)
– Netbooks (a small laptop computer designed primarily for
accessing Internet-based applications.)
– MP4 players
– Internet TVs
The Android Market
• In August 2008, Google announced Android Market, an online application store for Android devices.
• Using this app that is preinstalled on their Android device, users can simply download third-party applications.
• Both paid and free applications are supported on the Android Market.
• This app is currently known as Google Playstore
Architecture of Android
Architecture of Android
The Android OS is roughly divided into five sections in four main layers:
• Linux kernel —
– This is the kernel on which Android is based.
– Provides core services
• Security
• Memory management.
• Process management.
• File and network I/O
• Device drivers.
– Android specific services
• Power management
• Memory sharing
• Low memory killer
• IPC
• Libraries —
– Native Libraies written in C and C++.
1. System C Library 5 Surface manager
2. Media framework 6 WebKit 7 SQLite
3. OpenGL (Graphics engine) 8 SSL (Secure Socket Layer) for internet security
4. SGL (Scalable Graphics Library) 9 FreeType (Font Rendering)
Android runtime
At the same layer as the libraries, the Android runtime provides
1. A set of core libraries that enable developers to write Android apps using the Java
programming language (java.*, javax.*, android.*, org.*, junit.*).
2. Dalvik virtual machine.
• Dalvik is a specialized virtual machine designed specifically for Android.
• WorkFlow –
– Appliations are written in java.
– Compiled to java bytecodes.
– DX converts (compiles) java bytecodes to a single DEX bytecode file (Dalvik
executables).
– DVM executes DEX bytecode file.
• DVM enables every Android application to run in its own process, with its own instance of the
Dalvik virtual machine.
• It is optimized for battery-powered mobile devices with limited memory and CPU.
Application Framework
Exposes the various capabilities of the Android OS to application developers so that
they can make use of them in their applications.
• Package Manager - keeps track of all apps currently installed on phone.
• Window Manager – manages windows and sub windows.
• View System – Buttons, Icons, etc.
• Resource Manager – manages the non-compiled resources of an app. E.g. Strings
• Activity Manager – supports navigation between many activities of an app.
• Content Provider – they are databases that allow apps to store and share data,
developed to work across applications. Eg: Contacts.
• Location Manager – allows apps to receive location and movement information
(GPS) to do context specific tasks.
• Notification Manager – allows apps to place information in the notification bar.
Applications
• At this top layer, we will find applications that are built in with the Android device
(such as Phone, Contacts, Browser, etc.), as well as applications that we download
and install from the Android Market.
• None of these apps are hardcoded into the system.
REQUIRED TOOLS• Android development can be done on a Mac, a Windows PC, or
a Linux machine.
• Android makes use of the Java SE Development Kit (JDK).
Hence the development system should have JDK installed.
• The most important piece of software we need to download is
the Android SDK. It contains a debugger, libraries, an
emulator, documentation, sample code, and tutorials.
• It can be download from
http://developer.android.com/sdk/index.html
REQUIRED TOOLS
• Next tool/IDE that is required is Eclipse IDE.
• Important software that is needed Android Development Tools (ADT).
• The ADT is an extension to the Eclipse IDE that supports the creation and
debugging of Android applications.
• Using the ADT, you will be able to do the following in Eclipse:
➤ Create new Android application projects.
➤Access the tools for accessing your Android emulators and devices.
➤ Compile and debug Android applications.
➤ Export Android applications into Android Packages (APKs).
➤ Create digital certificates for code-signing your APK.
Creating an Android Project
To create an Android project we need to supply the information:
• Project name The name of the project (used by Eclipse)
• Application name A user-friendly name for our application. (To be
displayed in playstore)
• Package name The name of the package. we should use a reverse
domain name for this.
• Create Activity The name of the first activity in our application
• Min SDK Version The minimum version of the SDK that our project
is targeting
Activity
• In Android, an activity is a window that contains the user interface of
your applications.
• An application can have zero or more activities.
• In Helloworld example, the application contains one activity:
MainActivity.
•
• MainActivity is the entry point of the application, which is displayed
when the application is started.
ANATOMY OF AN ANDROID APPLICATION
Various folders and their files in package explorer in eclipse:
• Src — Contains the .java source files for your project.
• gen — Contains the R.java file, this file should not be modified . All
the resources in the project are automatically compiled into this class.
We can refer to them using the class called R.
• Android 4.4.2 library — This item contains one file android.jar,
which contains all the class libraries needed for an Android
application.
• assets — This folder contains all the assets used by our application,
such as HTML, text files, databases, etc.
ANATOMY OF AN ANDROID APPLICATION
• bin — This folder contains the files built by the ADT during the build
process. In particular, it generates the .apk file (Android Package). A
.apk file is the application binary of an Android application. It
contains everything needed to run an Android application.
• res — This folder contains all the resources used in the application. It
also contains a few other subfolders: drawable-<resolution>, layout,
and values.
• AndroidManifest.xml — This is the manifest file for your Android
application. Here one can specify the permissions needed by the
application, as well as other features (such as intent-filters, receivers,
etc.).
Android Virtual Devices
• An AVD is an emulator instance that enables us to model an actual
device.
• An Android Virtual Device (AVD) is used for testing Android
applications.
• Each AVD consists of a hardware profile; a mapping to a system
image; as well as emulated storage, such as a secure digital (SD) card.
• We can create as many AVDs as we want in order to test applications
with several different configurations.
• This testing is important to confirm the behaviour of our application
when it is run on different devices with varying capabilities.
Android Virtual Devices
Pros
• AVD is cheaper, we don't have to buy all the devices we want to test on.
• Easy to configure hardware like the size of the sd card, the display size
etc.
• We don't have to worry that our testing will mess up our phone or its
data or configuration.
Cons
• Android Emulator is pretty slow and that can be frustrating when we're
trying to rapidly experiment.
• Also some features are not well supported by the emulator. Ex: no
support for bluetooth connectivity and USB port.
• The emulator is not a device. We cannot guarantee how our application
is going to look and perform on an actual device.
API LevelAPI Level is an integer value that uniquely identifies the framework
API revision offered by a version of the Android platform.
The Android platform provides a framework API that applications can
use to interact with the underlying Android system. The framework
API consists of:
– A core set of packages and classes
– A set of XML elements and attributes for declaring a manifest file.
– A set of XML elements and attributes for declaring and accessing
resources.
– A set of Intents.
– A set of permissions that applications can request. (For example, an app
might want permission to see your device contacts or location)
– API is for development, so the changes in API version are more "inside".
New version of Android adds more features for users, that are "visible".
API Levelandroid:minSdkVersion
• An integer designating the minimum API Level required for the
application to run.
• The Android system will prevent the user from installing the
application if the system's API Level is lower than the value specified
in this attribute. You should always declare this attribute.
android:targetSdkVersion
• An integer designating the API Level that the application targets. If
not set, the default value equals that given to minSdkVersion.
• If the API level of the platform is higher than the version declared by
your app's targetSdkVersion, the system may enable compatibility
behaviors to ensure that your app continues to work the way you
expect.
AndroidManifest.xml fileIt contains detailed information about the application:
➤ It defines the package name of the application
➤ The version code of the application is 1 by default. It can be used to
programmatically determine whether an application needs to be upgraded.
➤ The version name of the application is 1.0. This string value is mainly used
for display to the user.
➤ The android:minSdkVersion attribute of the <uses-sdk> element specifies the
minimum version of the OS on which the application will run.
➤ The application uses the image named ic_launcher.png located in the
drawable folders.
➤ The name of this application is the string named app_name defined in the
strings.xml file.
AndroidManifest.xml fileThere is one activity in the application represented by the MainActivity.java file.
• The label displayed for this activity is the same as the application name.
• Within the definition for this activity, there is an element named <intent-
filter>:
➤ The action for the intent filter is named android.intent.action.MAIN
to indicate that this activity serves as the entry point for the
application.
➤ The category for the intent-filter is named
android.intent.category.LAUNCHER to indicate that the application
can be launched from the device’s launcher icon.
setContentView() methodFinally, the code that connects the activity to the UI (activity_main.xml) is
the setContentView() method, which is in the HelloWorldActivity.java file:
setContentView(R.layout.activity_main);
• Here, R.layout.activity_main refers to the activity_main.xml file located in
the res/layout folder.
• As we add additional XML files to the res/layout folder, the filenames will
automatically be generated in the R.java file.
R.Java (Autogenerated file)package com.example.HelloWorld;
public final class R {
public static final class attr { }
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
public static final class id {
public static final int button1=0x7f05002d;
} }
Strings.xml file• Strings are typically stored in an XML file in the res/values directory
• The format is an XML string tag containing a name attribute and then
containing the actual string itself.
• The actual string can include formatting and styling information, e.g.
HTML tags.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">hr</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
Strings.xml file
• It is recommended that we store all the string constants in our
application in this strings.xml file and reference these strings using the
@string identifier.
• Other resource files can refer to the strings that we've defined as:
@string/string_name
• In Java code, we can also access these strings, but this time we do it as
R.string.string_name.
• If we ever need to localize our application to another language, all we
need to do is make a copy of the entire values folder and modify the
values of strings.xml to contain the string in the required language.
Strings.xml file