Chapter One
Chapter One
CHAPTER ONE
Introduction to
Mobile Development
s c)
D .(M 4
mu 02
le , 2
t : Ke c 20
I n s e: D e
t
Da
What is Android
Android is open-source software packages and Linux based operating system for mobile devices such
as tablet computers, smartphones and Television.
And also, it is a mobile operating system based on a modified version of the Linux kernel and other
open source software, designed primarily for touchscreen mobile devices such as smartphones and
tablets.
Hence, Android is developed by a consortium of developers known as the Open Handset
Alliance(OHA), with the main contributor and commercial marketer being google.
Therefore, it is developed by Google and later the OHA (Open Handset Alliance) which means a
consortium of 84 companies such as google, samsung, synaptics, Garmin, Teleca, Ebay, Intel etc.
The first beta version of the Android Software Development Kit (SDK) was released by Google in 2007
where as the first commercial version, Android 1.0, was released in September 2008.
On June 27, 2012, at the Google I/O conference, Google announced the next Android version, 4.1 Jelly
Bean.
What is Android
Android is an operating system and programming platform developed by Google for mobile phones and
other mobile devices, such as tablets.
It can run on many different devices from many different manufacturers.
Android includes a software development kit (SDK) that helps to write original code and assemble
software modules to create apps for Android users.
Android also provides a marketplace to distribute apps. All together, Android represents an ecosystem
for mobile apps.
Android Features
As the world's most popular mobile platform, Android powers hundreds of millions of mobile devices in
more than 190 countries around the world.
It has the largest installed base of any mobile platform and is still growing fast.
Every day another million users power up their Android-powered devices for the first time and start
looking for apps, games, and other digital content.
Why develop apps for Android
Android provides a touchscreen user interface (UI) for interacting with apps.
People use touch gestures such as swiping, tapping, and pinching to manipulate on-screen objects.
In addition to the keyboard, there's a customizable on-screen keyboard for text input. Android can also support game
controllers and full-size physical keyboards connected by Bluetooth or USB.
Why develop apps for Android
The SDK includes software libraries of prewritten code, a debugger, a device emulator, documentation, sample
code, and tutorials.
Use the SDK to create apps that look great and take advantage of the hardware capabilities available on each
Android-powered device.
To develop apps using the SDK, use the Java programming language to develop the app and Extensible
Markup Language (XML) files to describe data resources. By writing the code in Java and creating a single
app binary, you create an app that can run on both phone and tablet form factors. You can declare your UI in
lightweight sets of XML resources.
Android Stack
3. Process Management: The kernel oversees the creation, scheduling, and termination of processes
and threads. It allocates CPU time and resources to processes, ensuring that multiple applications can
run simultaneously without causing conflicts.
4. File System Operations: The kernel provides a file system interface that allows applications to read
from and write to storage devices, including internal storage and external SD cards. It also manages
file permissions and access control.
5. Security: The kernel enforces security policies, including user and group permissions. It ensures that
processes are isolated from each other and that sensitive data is protected. It also manages access to
hardware resources based on permissions and policies.
Linux Kernel
6. Power Management: The kernel is responsible for power management, including controlling the
CPU's power states and coordinating the use of power-saving features. It plays a critical role in
optimizing battery life on mobile devices.
7. Network Operations: The kernel handles network stack operations, including routing, packet
processing, and communication with network interfaces. It manages network sockets and
connections, allowing applications to communicate over the network.
8. I/O Operations: The kernel manages input and output operations for devices such as touchscreens,
keyboards, and external peripherals. It ensures that data from these devices is processed and made
available to applications.
Native Libraries
• Native libraries in the Android operating system play a critical role in enhancing performance,
providing hardware support, and enabling access to low-level system resources.
• These libraries are written in programming languages like C and C++ and are often utilized by the
Android system itself and Android app developers.
OpenGL ES Libraries: Android uses OpenGL for rendering graphics on the screen. The Android OS
includes native libraries for OpenGL ES (Embedded Systems), which are critical for 2D and 3D graphics
rendering.
Media Framework: Android has a comprehensive media framework that includes native libraries for
audio and video processing. These libraries enable tasks such as audio playback, video decoding, and
camera functionality.
Native Libraries
Hardware Abstraction Layer (HAL): Android relies on native libraries for hardware support through
the Hardware Abstraction Layer (HAL). Each hardware component, such as the camera, sensors, and
graphics hardware, has its HAL, allowing the Android system to interface with and control these
components.
SQLite: SQLite is a lightweight, embedded database engine used for managing and storing data on
Android devices. It provides native libraries for efficient database operations, making it suitable for many
Android applications that require local data storage.
Security Libraries: Android includes various security-related native libraries to implement features like
SSL/TLS for secure communications, cryptography, and secure boot mechanisms.
Wireless and Connectivity Libraries: Native libraries handle wireless communication, including Wi-Fi
and cellular network interactions. They also manage Bluetooth and other connectivity options.
Dalvik Virtual Machine
Dalvik was the runtime environment and virtual machine used by the Android operating system before
the transition to ART.
The DVM was responsible for executing Android applications (APK files).
• Dex Files: Android app code is written in Java and compiled into bytecode. In the case of Dalvik, this
bytecode is transformed into a specialized file format called Dalvik Executable (DEX) files. These
DEX files contain the bytecode instructions for the app.
• Just-In-Time Compilation (JIT): Dalvik used a just-in-time (JIT) compilation strategy. It converted
DEX bytecode into native machine code during app runtime. This allowed for improved execution
speed, as the native code could be executed more efficiently on the device's CPU.
DVM
Application Frameworks
In the Android operating system (OS) stack, application frameworks are an integral part of the
architecture that enables developers to build Android applications efficiently.
Activity Manager:
The Activity Manager framework manages the lifecycle of applications and user interface
components, known as Activities..
Window Manager:
The Window Manager framework is responsible for managing the device's display and handling
user interface windows. It determines the layout and positioning of application windows on the
screen and coordinates user input with the appropriate activities.
View System:
The View System framework provides the UI components (Views) that allow developers to create
user interfaces. It includes the View, ViewGroup, and Widget classes, enabling the construction of
interactive and responsive UIs.
Application Frameworks
Content Providers:
Content Providers allow applications to share data and access data from other apps.
They provide a structured way to interact with and manage data, including databases,
files, and other content.
Resource Manager:
The Resource Manager framework handles resources such as strings, images, layouts,
and other assets used by Android applications
Notification Manager:
The Notification Manager framework allows apps to display notifications to the user. It
manages notifications, including system-level notifications and those generated by apps.
Application Frameworks
Location Manager:
The Location Manager framework provides access to location-based services, allowing
applications to retrieve the device's location through GPS, Wi-Fi, or other location
providers.
Telephony Manager:
The Telephony Manager framework provides access to telephony-related information
and services, including network state, phone information, and telephony services like
making calls and sending SMS messages.
Package Installer:
The Package Installer framework is responsible for installing and updating Android
applications. It manages the installation process, including permissions and verification.
Application Frameworks
Media Framework:
The Media Framework provides tools and APIs for handling media-related tasks,
including audio and video playback, recording, and media streaming.
Sensors Framework:
The Sensors Framework gives access to various sensors built into Android devices, such
as accelerometers, gyroscopes, light sensors, and more. It allows apps to gather and
process sensor data.
Bluetooth Framework:
The Bluetooth Framework provides APIs for managing Bluetooth connectivity, enabling
the development of Bluetooth-enabled applications for tasks like pairing, data transfer,
and device communication.
Android Stack
I
Android Version
Android Version
Android Core Building Blocks
The core building blocks or fundamental components of android are:- activities, views,
intents, services, content providers, fragments and AndroidManifest.xml.
The basic components of any android application are as follows:
2. Services 9. SharedPreferences
• An activity goes through a number of states. Activity lifecycle in Android manages the
different states of activity, such as when the activity starts or stops, etc. All of these states
are managed by callback methods.
Activity life cycle
onCreate(): Called when the activity is first created. This is where you should do all of your
normal static set up: create views, bind data to lists, etc. This method also provides you with
a Bundle containing the activity's previously frozen state, if there was one. Always
followed by onStart().
Activity life cycle
onStart: Called when the activity is becoming visible to the user. Followed by onResume()
if the activity comes to the foreground, or onStop() if it becomes hidden.
onReStart: Called after your activity has been stopped, prior to it being started again.
Always followed by onStart().
Activity life cycle
onResume: Called when the activity will start interacting with the user. At this point your
activity is at the top of its activity stack, with user input going to it. Always followed by
onPause().
onPause(): Called when the activity loses foreground state, is no longer focusable or before
transition to stopped/hidden or destroyed state.Followed by either onResume() if the
activity returns back to the front, or onStop() if it becomes invisible to the user.
Activity life cycle
onStop: Called when the activity is no longer visible to the user. This may happen either
because a new activity is being started on top, an existing one is being brought in front of
this one, or this one is being destroyed. This is typically used to stop animations and
refreshing the UI, etc.
onDestroy: The final call you receive before your activity is destroyed. This can happen
either because the activity is finishing
Activity life cycle
2. Services
A service is a component which runs in the background without direct interaction with the
user.
Android service is a component that is used to perform operations on the background such
as playing music, handle network transactions, interacting content providers etc.
As the service has no user interface, it is not bound to the lifecycle of an activity.
They handle background processing associated with an application.
There are two types of services local and remote.
Local service is accessed from within the application whereas remote service accessed
remotely from other applications running on the same device.
Type of Services
Bound Service
A service is bound when an application component binds to it by calling bindService().
A bound service offers a client-server interface that allows components to interact with the
service, send requests, get results, and even do so across processes with inter-process
communication (IPC).
The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.
Services life cycle
Fragment
Fragments are like parts of activity. An activity can display one or more fragments on the
screen at the same time.
AndroidManifest.xml
It contains informations about activities, content providers, permissions etc. It is like the
web.xml file in Java EE.
Android Software Development kit(SDK)
• 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.
• Android Emulator -A virtual mobile device that runs on our computer -use to design,
debug, and test our applications in an actual Android run-time environment.
• The Android emulator provides almost all the functionality of a real device.
Installing the emulator
The Android emulator is installed while installing the Android Studio. However some
components of emulator may or may not be installed while installing Android Studio.
To install the emulator component, select the Android Emulator component in the SDK
Tools tab of the SDK Manager or click by AVD manager in the icon.
Dalvik Virtual Machine (DVM)
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.
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.
Android Application
Anatomy of Android Application
Before you run your app, you should be aware of a few directories and files in the
Android project.
AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app
and defines each of its components.
Java
This contains the .java source files for your project. By default, it includes an
MainActivity.java source file having an activity class that runs when your app is
launched using the app icon.
res/drawable-hdpi
This is a directory for drawable objects that are designed for high-density screens.
Anatomy of Android Application
res/layout
This is a directory for files that define your app's user interface.
res/values
This is a directory for other various XML files that contain a collection of resources, such as
strings and colours definitions.
Build.gradle
This is the actual application file which ultimately gets converted to a Dalvik executable and runs your
application.
Following is the default code generated by the application wizard for Hello World! Application
Whatever component we want develop as a part of our application, we must declare all its
components in a manifest.xml which resides at the root of the application project directory.
This file works as an interface between Android OS and your application, so if we do not
declare your component in this file, then it will not be considered by the OS.
For example, a default manifest file will look like as following file
The Manifest File
The Manifest File
The <activity> tag is used to specify an activity and android:name attribute specifies the
fully qualified class name of the Activity subclass and the android:label attributes specifies a
string to use as the label for the activity. You can specify multiple activities using <activity>
tags.
The Manifest File
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.
The @string refers to the strings.xml file explained below. Hence, @string/app_name refers
to the app_name string defined in the strings.xml file, which is "HelloWorld". Similar way,
other strings get populated in the application.
The Color File
The strings.xml file is located in the res/values folder and it contains all the text that our
application uses. A string is a simple resource that is referenced using the value provided in
the name attribute (not the name of the XML file). So, we can combine string resources
with other simple resources in the one XML file, under one <resources> element.
Elements:
<resources>
Required. This must be the root node.
No attributes.
<string>
A string, which can include styling tags. Beware that must escape
apostrophes and quotation marks.
attributes:
name
String. A name for the string. This name is used as the resource ID.
The Color File
A color value defined in XML. The color is specified with an RGB value and alpha channel.
We can use a color resource any place that accepts a hexadecimal color value. And also, we
can use a color resource when a drawable resource is expected in XML (for example,
android:drawable="@color/green").
The value always begins with a pound (#) character and then followed by the Alpha-
Red-Green-Blue information in one of the following formats:
#RGB
#ARGB
#RRGGBB
#AARRGGBB
The Color File
<color>
A color expressed in hexadecimal, as described above.
attributes:
name
String. A name for the color. This will be used as the resource ID.
example:
XML file saved at res/values/colors.xml:
The Layout File