5101:Mobile Application
Introduction to Android Programming Language
-----------------------------------------------------------------------------------------------------
What is android?
- Android is an open-source, Linux-based operating system used in touchscreen mobiles,
tablets, televisions, etc.
- It's the largest installed operating system of any mobile operating system and growing
rapidly day by day.
- You can develop apps and games using Android and it gives you an open marketplace for
distributing your apps and games instantly.
- Android is the operating system for powering screens of all sizes.
- It is developed by Google and later the OHA (Open Handset Alliance).
- Java language is mainly used to write the android code even though other languages can be
used.
- The goal of android project is to create a successful real-world product that improves the
mobile experience for end users.
Features of Android:
1) It is open-source.
2) Anyone can customize the Android Platform.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It contains a wide range of media supports like AVI, MKV, FLV, MPEG4, etc. to play or record a
variety of audio/video.
5) It provides support for messaging services(SMS and MMS), web browser, storage (SQLite),
media, handset layout etc.
6) Android supports different types of connectivity for GSM, CDMA, Wi-Fi, Bluetooth, etc. for
telephonic conversation or data transfer.
7) Using wifi technology we can pair with other devices while playing games or using other
applications.
8) It also supports different image formats like JPEG, PNG, GIF, BMP, MP3, etc.
5101:Mobile Application
9) Android supports multi-tasking means we can run multiple applications at a time and can
switch between them.
What is Open Handset Alliance (OHA)?
It's a consortium of 84 companies such as google, samsung, AKM, synaptics, KDDI, Garmin, Teleca,
Ebay, Intel etc.
It was established on 5th November, 2007, led by Google. It is committed to advance open
standards, provide services and deploy handsets using the Android Platform.
Categories of Android applications
There are many android applications in the market. The top categories are:
o Entertainment
o Tools
o Communication
o Productivity
o Personalization
o Music and Audio
o Social
o Media and Video
o Travel and Local etc.
What are the advantages of Android?
Open-source: It means no license, distribution and development fee.
Platform-independent: It supports Windows, Mac, and Linux platforms.
Supports various technologies: It supports camera, Bluetooth, wifi, speech, EDGE etc.
technologies.
Highly optimized Virtual Machine: Android uses a highly optimized virtual machine for
mobile devices, called DVM (Dalvik Virtual Machine).
5101:Mobile Application
History and Versions of Android
Android is the operating system for powering screens of all sizes.
The history and versions of android are interesting to know. Android version is named after a
dessert. The code names of android ranges from A to J currently, such
as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream
Sandwitch, Jelly Bean, KitKat and Lollipop etc.
Android History:
1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United
States in October, 2003.
2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in the
subsidiary of Google Incorporation.
3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris
White and Nick Sears.
4) Originally intended for camera but shifted to smart phones later because of low market
for camera only.
5) Android is the nick name of Andy Rubin given by coworkers because of his love to
robots.
6) In 2007, Google announces the development of android OS.
7) In 2008, HTC launched the first android mobile.
5101:Mobile Application
Android Versions, Codename and API and features
5101:Mobile Application
5101:Mobile Application
Android Architecture
android architecture or Android software stack is categorized into five parts:
1. Linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications
Fig: Android Architecture or Android Software Stack
5101:Mobile Application
1) Linux kernel
It is the heart of android architecture that exists at the root of android architecture.
The interaction of the hardware with the software is provided by means of Linux.
Linux kernel is responsible for device drivers, power management, memory
management, device management and resource access.
2) Native Libraries
On the top of linux kernel, there are Native libraries such as WebKit, OpenGL,
FreeType, SQLite, Media, C runtime library (libc) etc.
This layer provides the device, which has the capability to handle different types
of data and information.
These libraries are coded in C and C++ languages.
Some of the libraries are:
-The WebKit library is responsible for browser support,
-SQLite is for database,
-FreeType for font support,
-Media for playing and recording audio and video formats.
3) Android Runtime
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine)
which is responsible to run android application.
DVM is like JVM but it is optimized for mobile devices. It consumes less memory
and provides fast performance.
4) Application Framework
On the top of Native libraries and android runtime, there is application framework.
Android framework includes Android API's such as UI (User Interface), telephony,
resources, locations, Content Providers (data) and package managers.
It provides a lot of classes and interfaces for android application development.
The important blocks of Application framework are as follows:
5101:Mobile Application
Activity Manager :- Manages activity life cycle of applications.
Content provider :- Provides data sharing between applications
Telephony Manager :- Manages all voice calls (optional)
Location Manager :- Manages all activities related with location in your
application.
Resource manager :- Manages different kinds of resources in an application.
5) Applications
On the top of android framework, there are applications.
This is the place where your application is finally deployed over a device.
Some applications are installed by default in a device such as SMS client app, dialer,
Web browser, and contact manager.
--------------------------------------------------------------------------------------------------------------------
Dalvik virtual machine:
The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile
devices. It optimizes the virtual machine for memory, battery life, and performance.
Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.
Dalvik Virtual Machine is used in Android System similar to the usage of JVM in Java. Unlike
the JVM, the DVM does not recognize files with the .class extension; instead, it executes the
files with the .dex extension. The Dex compiler converts the class files into the .dex file that run
on the Dalvik VM. Multiple class files are converted into one dex file.
5101:Mobile Application
The javac tool compiles the java source file into the class file.
The dx tool takes all the class files of your application and generates a single .dex file. It is
a platform-specific tool.
The Android Assets Packaging Tool (aapt) handles the packaging process.
Advantages of DVM over JVM:
DVM takes less memory, runs and loads faster compared to JVM.
--------------------------------------------------------------------------------------------------------------------
5101:Mobile Application
Android Core Building Blocks
An android component is simply a piece of code that has a well defined life cycle e.g.
Activity, Receiver, Service etc.
The core building blocks or fundamental components of android are activities, views,
intents, services, content providers, fragments and [Link].
Activity
An activity is a class that represents a single screen. It is like a Frame in AWT.
View
A view is the UI element such as button, label, text field etc. Anything that you see is a
view.
Intent
Intent is used to invoke components. It is mainly used to:
o Start the service
o Launch an activity
o Display a web page
o Display a list of contacts
o Broadcast a message
o Dial a phone call etc.
5101:Mobile Application
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link]
startActivity(intent);
Service
Service is a background process that can run for a long time.
There are two types of services local and remote. Local service is accessed from within the
application whereas remote service is accessed remotely from other applications running
on the same device.
Content Provider
Content Providers are used to share data between the applications.
Fragment
Fragments are like parts of activity. An activity can display one or more fragments on the
screen at the same time.
[Link]
It contains informations about activities, content providers, permissions etc. It is like the
[Link] file in Java EE.
Android Virtual Device (AVD)
It is used to test the android application without the need for mobile or tablet etc. It can
be created in different configurations to emulate different types of real devices.
5101:Mobile Application
Android Emulator
The Android emulator is an Android Virtual Device
(AVD), which represents a specific Android device. We can
use the Android emulator as a target device to execute and
test our Android application on our PC. The Android
emulator provides almost all the functionality of a real
device. We can get the incoming phone calls and text
messages. It also gives the location of the device and
simulates different network speeds. Android emulator
simulates rotation and other hardware sensors. It accesses
the Google Play store, and much more
Testing Android applications on emulator are sometimes faster and easier than
doing on a real device. For example, we can transfer data faster to the emulator than to a
real device connected through USB.
The Android emulator comes with predefined configurations for several Android phones,
Wear OS, tablet, Android TV devices.
Requirement and recommendations
The Android emulator takes additional requirements beyond the basic system requirement
for Android Studio. These requirements are given below:
o SDK Tools 26.1.1 or higher
o 64-bit processor
o Windows: CPU with UG (unrestricted guest) support
o HAXM 6.2.1 or later (recommended HAXM 7.2.0 or later)
Run an Android app on the Emulator
We can run an Android app form the Android Studio project, or we can run an app which
is installed on the Android Emulator as we run any app on a device.
5101:Mobile Application
To start the Android Emulator and run an application in our project:
1. In Android Studio, we need to create an Android Virtual Device (AVD) that the
emulator can use to install and run your app. To create a new AVD:-
1.1 Open the AVD Manager by clicking Tools > AVD Manager.
1.2 Click on Create Virtual Device, at the bottom of the AVD Manager dialog. Then Select
Hardware page appears.
1.3 Select a hardware profile and then click Next. If we don’t see the hardware profile we want,
then we can create or import a hardware profile. The System Image page appears.
5101:Mobile Application
1.4 Select the system image for the particular API level and click Next. This leads to open a Verify
Configuration page.
1.5 Change AVD properties if needed, and then click Finish.
5101:Mobile Application
2. In the toolbar, choose the AVD, which we want to run our app from the target device from the
drop-down menu.
3. Click Run.
Launch the Emulator without first running an app
To start the emulator:
1. Open the AVD Manager.
2. Double-click an AVD, or click Run
While the emulator is running, we can run the Android Studio project and select the
emulator as the target device. We can also drag an APKs file to install on an emulator, and
then run them.
Start the emulator from the command line
Here is the basic command-line syntax for starting a virtual device:
$ emulator @avd_name [ {-option [value]} ... ]
To display the list of AVD names, enter the following command:
$ emulator -list-avds
Run and stop an emulator, and clear data
From the Virtual Device page, we can perform the following operation on emulator:
5101:Mobile Application
o To run an Android emulator that uses an AVD, double-click the AVD, or
click Launch
o To stop the running emulator, right-click and select Stop, or click Menu ▼ and
select Stop.
o If we want to clear the data from an emulator and return it to the initial state when
it was first defined, then right-click an AVD and select Wipe Data. Or click menu ▼
and select Wipe Data.
5101:Mobile Application
Building a sample Android App using Android Studio
Hello Android Example
You need to follow the 3 steps for creating the Hello android application.
1) Create the New Android project
For creating the new android studio project:
1) Select Start a new Android Studio project
2) Provide the following information: Application name, Company domain, Project location and
Package name of application and click next.
5101:Mobile Application
3) Select the API level of application and click next.
4) Select the Activity type (Empty Activity).
5101:Mobile Application
5) Provide the Activity Name and click finish.
After finishing the Activity configuration, Android Studio auto generates the activity class
and other required configuration files.
Now an android project has been created. You can explore the android project and see the
simple program, it looks like this:
5101:Mobile Application
2) Write the message
File: activity_main.xml
Android studio auto generates code for activity_main.xml file. You may edit this file
according to your requirement.
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
k/res/android"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Android!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
}
File: [Link]
package [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
5101:Mobile Application
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
3) Run the android application
To run the android application, click the run icon on the toolbar or simply press Shift + F10
The android emulator might take 2 or 3 minutes to boot. So please have patience. After
booting the emulator, the android studio installs the application and launches the activity.
You will see something like this:
5101:Mobile Application
Internal Details of Hello Android Example
Android application contains different components such as java source code, string resources,
images, manifest file, apk file etc.
Let's understand the project structure of android application.
Java Source Code
File: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {//(1)
@Override
protected void onCreate(Bundle savedInstanceState) {//(2)
5101:Mobile Application
[Link](savedInstanceState);
setContentView([Link].activity_main);//(3)
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {//(4)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}
}
(1) Activity is a java class that creates and default window on the screen where we can place
different components such as Button, EditText, TextView, Spinner etc. It is like the Frame of Java
AWT.
It provides life cycle methods for activity such as onCreate, onStop, OnResume etc.
(2) The onCreate method is called when Activity class is first created.
(3) The setContentView([Link].activity_main) gives information about our layout
resource. Here, our layout resources are defined in activity_main.xml file.
File: activity_main.xml
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
5101:Mobile Application
As you can see, a textview is created by the framework automatically. But the message for
this string is defined in the [Link] file. The @string/hello_world provides information
about the textview message. The value of the attribute hello_world is defined in the
[Link] file.
File: [Link]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">helloandroid</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
</resources>
Generated [Link] file
It is the auto-generated file that contains IDs for all the resources of res directory. It is
generated by aapt(Android Asset Packaging Tool). Whenever you create any component
on activity_main, a corresponding ID is created in the [Link] file which can be used in the
Java Source file later.
File: [Link]
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package [Link];
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 id {
public static final int menu_settings=0x7f070000;
}
public static final class layout {
5101:Mobile Application
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
public static final int menu_settings=0x7f040002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/[Link] on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/[Link], while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/[Link] on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/[Link] and
res/values-v11/[Link] on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f050000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f050001;
}
}
5101:Mobile Application
APK File
An apk file is created by the framework automatically. If you want to run the android
application on the mobile, transfer and install it.
Resources
It contains resource files including activity_main, strings, styles etc.
Manifest file
It contains information about package including components such as activities, services,
content providers etc.
It performs some other tasks also:
o It is responsible to protect the application to access any protected parts by
providing the permissions.
o It also declares the android api that the application is going to use.
o It lists the instrumentation classes. The instrumentation classes provides profiling
and other informations. These informations are removed just before the application
is published etc.
This is the required xml file for all the android application and located inside the root
directory.
A simple [Link] file looks like this:
<manifest xmlns:android="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
5101:Mobile Application
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
Elements of the [Link] file
<manifest>
manifest is the root element of the [Link] file. It has package attribute that
describes the package name of the activity class.
<application>
application is the subelement of the manifest. It includes the namespace declaration. This element
contains several subelements that declares the application component such as activity etc.
The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
android:theme represents a common theme for all the android activities.
5101:Mobile Application
<activity>
activity is the subelement of application and represents an activity that must be defined in
the [Link] file. It has many attributes such as label, name, theme,
launchMode etc.
android:label represents a label i.e. displayed on the screen.
android:name represents a name for the activity class. It is required attribute.
<intent-filter>
intent-filter is the sub-element of activity that describes the type of intent to which
activity, service or broadcast receiver can respond to.
<action>
It adds an action for the intent-filter. The intent-filter must have at least one action
element.
<category>
It adds a category name to an intent-filter.