0% found this document useful (0 votes)
21 views73 pages

1 Introduction

The document provides an overview of Android programming, covering its history, architecture, fundamental components, and features. It details the evolution of Android from its inception to its current status as a leading mobile operating system, including its open-source nature and the role of the Open Handset Alliance. Additionally, it describes the core building blocks of Android applications, such as Activities, Services, and the Activity lifecycle.

Uploaded by

Mahesh Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views73 pages

1 Introduction

The document provides an overview of Android programming, covering its history, architecture, fundamental components, and features. It details the evolution of Android from its inception to its current status as a leading mobile operating system, including its open-source nature and the role of the Open Handset Alliance. Additionally, it describes the core building blocks of Android applications, such as Activities, Services, and the Activity lifecycle.

Uploaded by

Mahesh Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

SEC: Android Programming

Introduction

Mahesh Kumar
(maheshkumar@andc.du.ac.in)

Course Web Page


(www.mkbhandari.com/mkwiki)
Outline

1 History of Android

2 Introduction to Android Operating System

3 Android Architecture

4 Android Fundamental Components (Core Building Blocks)


History of Android
From its inaugural release to today, Android has transformed visually,
conceptually and functionally.

Let's understand the android history in a sequence.


→ Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United States in
October, 2003.

→ In 17th August 2005, Google acquired Android Incorporation. Since then, it is in the
subsidiary of Google Incorporation.

→ The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris White and
Nick Sears.

→ Originally intended for camera but shifted to smart phones later because of low market
for camera only.
History of Android
→ Android is the nick name of Andy Rubin given by coworkers because of his love to
robots.

→ In 2007, Google announces the development of Android OS.

→ In 2008, HTC launched the first Android mobile (T-Mobile G1).

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.
History of Android – Versions, Codename, API Level

Android 12 12 October 4, 2021 Yes 31 [2]


Introduction to Android Operating System
Android 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.

Android is written in Java (UI), C (core), C++ and others.

Android is developed by a consortium of developers known as the Open


Handset Alliance and commercially sponsored by Google.
Introduction to Android Operating System
It was unveiled in November 2007, with the first commercial Android device
launched in September 2008.

It is free and open source software; its source code is known as Android
Open Source Project (AOSP), which is primarily licensed under the Apache
License.

However most Android devices ship with additional proprietary software


pre-installed, most notably Google Mobile Services (GMS) which includes
core apps such as Google Chrome, the digital distribution platform Google
Play and associated Google Play Services development platform.
Introduction to Android Operating System
About 70 percent of Android smartphones run Google's ecosystem

The "Android" name and logo are trademarks of Google which impose
standards to restrict "uncertified" devices outside their ecosystem to use
Android branding.

The source code has been used to develop variants of Android on a range of
other electronics, such as game consoles, digital cameras, portable media
players, PCs and others, each with a specialized user interface.

Some well known derivatives include Android TV for televisions and Wear OS
for wearables, both developed by Google.
Introduction to Android Operating System
Android has been the best-selling OS worldwide on smartphones since 2011
and on tablets since 2013.

As of May 2017, it has over two billion monthly active users, the largest
installed base of any operating system

As of August 2020, the Google Play Store features over 3 million apps.

The current stable version is Android 12, released on October 4, 2021.

The goal of android project is to create a successful real-world product that


improves the mobile experience for end users.
Why Android?

[3]
Features of Android
1 Beautiful UI :
Android OS basic screen provides a beautiful and intuitive user interface.

2 Connectivity :
GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX.

3 Storage :
SQLite, a lightweight relational database, is used for data storage purposes.

4 Media support :
H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg
Vorbis, WAV, JPEG, PNG, GIF, and BMP.
5 Messaging :
SMS and MMS.
Features of Android
6 Web Browser :
Based on the open-source WebKit layout engine, coupled with Chrome's V8
JavaScript engine supporting HTML5 and CSS3

7 Multi-touch :
Android has native support for multi-touch which was initially made available in
handsets such as the HTC Hero.

8 Multi-tasking :
User can jump from one task to another and same time various application can
run simultaneously.

9 Resizable widgets :
Widgets are resizable, so users can expand them to show more content or shrink
them to save space.
Features of Android
10 Multi-Language :
Supports single direction and bi-directional text.

11 GCM :
Google Cloud Messaging (GCM) is a service that lets developers send short
message data to their users on Android devices, without needing a proprietary
sync solution.

12 Wi-Fi Direct :
A technology that lets apps discover and pair directly, over a high-bandwidth
peer-to-peer connection.

13 Android Beam :
A popular NFC-based technology that lets users instantly share, just by touching
two NFC-enabled phones together.
Android Applications
Many, to almost all, Android devices come with preinstalled Google apps
including Gmail, Google Maps, Google Chrome, YouTube, Google Play Music,
Google Play Movies & TV, and many more.
Applications ("apps"), are written using the Android software development kit
(SDK).

Kotlin programming language was originally announced in May 2017, which


replaced Java as Google's preferred language for Android app development
in May 2019.

Java is still supported (originally the only option for user-space programs,
and is often mixed with Kotlin), as is C++.
Java and/or other JVM languages, such as Kotlin, may be combined with C/
C++.
Android Applications
Once developed, Android applications can be packaged easily and sold out
either through a store such as:
→ Google Play
→ SlideME
→ Opera Mobile Store
→ Mobango
→ F-droid
→ Amazon Appstore
Categories of Android Applications
There are many android applications in the market. The top categories are

[3]
Android Architecture
Android is an open source, Linux-based software stack created for a wide
array of devices and form factors.

Android architecture or Android software stack is categorized into five parts:


1 Linux Kernel

2 Native Libraries (middleware)

3 Android Runtime

4 Application Framework (Java API Framework)

5 Applications (System Apps)


Android Architecture

[3]
Linux Kernel
The foundation of the Android platform is the Linux kernel.

It is the heart of android architecture that exists at the root of android


architecture.

Linux kernel is responsible for


→ Device drivers

→ Power Management

→ Memory Management

→ Device Management
→ Resource Access
Native C/C++ Libraries
On the top of Linux kernel, their are Native libraries such as :
→ Webkit - for browser support ( open source web browser )

→ OpenGL – graphics library used to produce 2D/3D computer graphics (cross-


language, cross-platform)

→ FreeType – for font support

→ SQLite – for database

→ Media – for playing and recording audio and video formats.

→ Libc - C runtime library

→ SSL – for internet security


Android Runtime (ART)
The third section of the architecture - which is responsible to run Android application.

Available on the second layer from the bottom.

ART provides a key component called Dalvik Virtual Machine (DVM), a kind of JVM
specially designed and optimized for Android.

DVM make use of Linux core features like memory management and multithreading
which are integral part of Java language.

DVM enables every Android application to run in its own process, with its own
instance of DVM.

In addition to DVM, ART also provides a set of core libraries that enables Android app
developers to write/develop Android apps using standard Java Language
Android Runtime (ART) – Core Libraries
Application (Java API) Framework
On the top of Native libraries and Android runtime, there is Application
framework

Application framework includes Android API's such as UI (User Interface),


telephony, resources, locations, Content Providers (data) and package
managers.

Application framework provides many higher-level services to applications in


the form of Java classes.

App developers are allowed to make use of these services in their


applications.
Application (Java API) Framework
Key services are:
1 Activity Manager: Controls all aspects of the application life-cycle & activity stack.

2 Content Providers: Allows apps to publish & share data with other apps.

3 Resource Manager: Provides access to non-code embedded resources such as


strings,color settings, and user interface layouts.

4 Notification Manager: Allows apps to display alerts & notifications to the users.

5 View System: An extensible set of views used to create application UI’s

6 Location Manager: Finds the device’s geographic locations.


Applications
On the top of android framework, there are applications.

App developers can only develop applications for this layer.

The pre-installed applications like home, contacts, camera, gallery etc and
third party applications downloaded from the play store like chat
applications, games etc. will be installed on this layer only.

All applications such as home, contact, settings, games, browsers are using
Application Framework that uses Android Runtime and Native Libraries.

Android Runtime and Native Libraries are using Linux Kernel.


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 :


1 Activities 6 Fragments

2 Views 7 AndroidManifest.xml

3 Intents and Broadcast Receivers

4 Services

5 Content Providers
Android Core Building Blocks

[4]
Activity
An activity is the entry point for interacting with the user. It represents a
single screen with a user interface.
→ For example: An email app might have one activity that shows a list of new
emails, another activity to compose an email, and another activity for reading
emails.

Although the activities work together to form a cohesive user experience in


the email app, each one is independent of the others.
→ As such, a different app can start any one of these activities if the email app
allows it.

→ For example, a camera app can start the activity in the email app that
composes new mail to allow the user to share a picture.
Activity
An activity facilitates the following key interactions between system and app:
→ Keeping track of what the user currently cares about (what is on screen) to
ensure that the system keeps running the process that is hosting the activity.

→ Knowing that previously used processes contain things the user may return to
(stopped activities), and thus more highly prioritize keeping those processes
around.

→ Helping the app handle having its process killed so the user can return to
activities with their previous state restored.

→ Providing a way for apps to implement user flows between each other, and for
the system to coordinate these flows. (The most classic example here being
share.)
Activity
The first stepping stone in building an Android user app.

Activity provides the space to the user for doing anything and everything.
→ For example: opening a contact, dialing a caller etc., everything is done by the activity
with a window and that every window is provided by an activity.

A window is provided to each activity where user interfacing is done.

An activity is a class that represents a single screen. It is like a Frame in AWT

Generally, an Android app has more than one activity.


→ There is one “main” activity, and all other activities are “child” activities.
Activity
An activity is implemented as a subclass of class Activity.

public class MainActivity extends Activity {

//do something

}
Activity
A task is a collection of activities that users interact with when performing a
certain job. For example: Buying a product (mobile phone) from Amazon app.

The activities are arranged in a stack (the Back Stack) in the order in which
each activity is opened.

A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the
current activity is destroyed and the previous activity resumes. [5]
Activity Lifecycle
As a user navigates through, out of, and back to your app, the Activity
instances in your app transition through different states in their lifecycle.

The Activity class provides a number of callbacks that allow the activity to
know that a state has changed: that the system is creating, stopping, or
resuming an activity, or destroying the process in which the activity resides.

Within the lifecycle callback methods, you can declare how your activity
behaves when the user leaves and re-enters the activity.
→ For example, if you're building a streaming video player, you might pause the video
and terminate the network connection when the user switches to another app.
When the user returns, you can reconnect to the network and allow the user to
resume the video from the same spot.
Activity Lifecycle
Each callback allows you to perform specific work that's appropriate to a
given change of state.

Doing the right work at the right time and handling transitions properly make
your app more robust and performant.

For example, good implementation of the lifecycle callbacks can help ensure
that your app avoids:
→ Crashing if the user receives a phone call or switches to another app while using
your app.
→ Consuming valuable system resources when the user is not actively using it.
→ Losing the user's progress if they leave your app and return to it at a later time.
→ Crashing or losing the user's progress when the screen rotates between landscape
and portrait orientation.
Activity Lifecycle
In C, C++, or Java program starts from main( ) function.

Very similar way, Android system initiates its program with in an Activity
starting with a call on onCreate( ) callback method.

There is a sequence of callback methods that start up an activity and a


sequence of callback methods that tear down an activity as shown in the
Activity life cycle diagram (in next slide):
A simplified illustration of the activity lifecycle. [5]
Activity Lifecycle
To navigate transitions between stages of the activity lifecycle, the Activity
class provides a core set of six callbacks:
1 onCreate( ) : This is the first callback and called when the activity is first created.

2 onStart( ) : This callback is called when the activity becomes visible to the user.

3 onResume( ) : This is called when the user starts interacting with the application.

4 onPause( ) : The paused activity does not receive user input and cannot execute
any code and called when the current activity is being paused and the previous
activity is being resumed.
5 onStop( ) : This callback is called when the activity is no longer visible.

6 onDestroy( ) : This callback is called before the activity is destroyed by the system.

The system invokes each of these callbacks as an activity enters a new state.
Activity Lifecycle - Summary
Open/Launch app – onCreate( ), onStart( ), onResume( )

Minimize app – onPause( ), onStop( )

Recent apps (Go back to app after minimize) – onRestart( ), onStart( ),


onResume( )

Close app - onPause( ), onStop( ), onDestroy( )


Service
A Service is an application component that can perform long-running operations in
the background.

It does not provide a user interface.

Once started, a service might continue running for some time, even after the user
switches to another application.

Additionally, a component can bind to a service to interact with it and even perform
inter-process communication (IPC).

For example, a service can:


→ Handle network transactions
→ Play music All from the background
→ Perform file I/O
→ Interact with a content provider
Service
A service is implemented as a subclass of Service

public class ServiceName extends Service {

//do something

}
Types of Services
Three different types of services:
Types of Services
1 Foreground Service:
A foreground service performs some operation that is noticeable to the user.
Users can interact with the service by the notifications provided about the
ongoing task.
For example:
→ An audio app would use a foreground service to play an audio track.

→ Downloading a file, the user can keep track of the progress in downloading and can also
pause and resume the process.

Foreground services must display a Notification, so that users are actively aware
that the service is running. This notification cannot be dismissed unless the
service is either stopped or removed from the foreground.
Foreground services continue running even when the user isn't interacting with
the app.
Types of Services
2 Background Service:
A background service performs an operation that isn't directly noticed by the
user.

Background services do not require any user intervention.

These services do not notify the user about ongoing background tasks and
users also cannot access them.

For example:
→ If an app used a service to compact its storage, that would usually be a background
service.
→ The process like schedule syncing of data or storing of data
Types of Services
3 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, receive results, and even do so across
processes with interprocess communication (IPC).

A bound service runs only as long as another application component is bound


to it.

Multiple components can bind to the service at once, but when all of them
unbind, the service is destroyed.
The Lifecycle of Android Services
Content Providers
A content provider manages a shared set of app data that you can store in the file
system, in a SQLite database, on the web, or on any other persistent storage location
that your app can access.

Through the content provider, other apps can query or modify the data if the content
provider allows it.

For example, the Android system provides a content provider that manages the user's
contact information. As such, any app with the proper permissions can query the content
provider, such as ContactsContract.Data, to read and write information about a particular
person.

Content providers encapsulate the data, and provide mechanisms for defining data
security of an android app.
Content Providers
Content Providers
Basically content providers provides contents of one process to another; hence acting
as an interface.

In order to access data in a content provider, an object has to be created, which act as
a client whereas content provider act as a server.

It is the object which is going to receive requests, retrieve results and returns the
results.

In a nutshell, the role of the content provider in the android system is like a central
repository in which data of the applications are stored, and it facilitates other
applications to securely access and modify that data based on the user requirements.
Content Providers
Overview diagram of how content providers manage access to storage.
Content Providers
Illustration of migrating content provider storage.
Content Providers
Lastly, content provider can be used to manage access to a variety of data storage
sources, including both:

→ Structured data (such as a SqLite relational database)

→ Unstructured data (such as image files)

A content provider is implemented as a subclass of ContentProvider

public class ContentProviderName extends ContentProvider {

//do something

}
Intents
Basically, an intent is a messaging object which is used to request an action from
another app component , such as, an intent can be used to:
→ Start/stop an activity

→ Get results from another activity

→ Start a service

→ Broadcast messages system-wide


→ Request an action to be performed on a particular piece of data etc.

Although intents facilitate communication between components in several ways,


there are three fundamental use cases:
1 Starting an activity
2 Starting a service
3 Delivering a broadcast
Intents
1 Starting an activity:

You can start a new instance of an Activity by passing an Intent object to


startActivity() method. The Intent object describes the activity to start and
carries any necessary data.

To receive a result from the activity when it finishes, call startActivityForResult()


method. Then the activity receives the result as a separate Intent object in the
activity's onActivityResult() method.

2 Starting a service:

You can start a service to perform a one-time operation (such as downloading a


file) by passing an Intent object to startService() method. The Intent object
describes the service to start and carries any necessary data.
Intents
If you want to create a bound service (client-server interface), you can bind the
service to a component by passing an Intent object to bindService() method.

3 Delivering a broadcast:

The system delivers various broadcasts for system events, such as when the
system boots up or the device starts charging

You can deliver a broadcast to other apps by passing an Intent to:


→ sendbroadcast() method

→ sendOrderedBroadcast() method
Intents
An Intent is created with an Intent object, which defines a message to activate either:
→ Specific component ( explicit intent)

→ Specific type of component ( implicit intent)

Types of Intents:
1 Explicit intents - user knows exactly which activity can handle the requests.

2 Implicit intents - declare general action to perform


Intents
1 Explicit intents

Specify which application will satisfy the intent, by supplying either the target
app's package name(i.e., apk) or a fully-qualified component class name.

You typically use an explicit intent to start a component in you own app, since
you know the class name of the desired activity or service to start.

For example, you might start a service to download a file in the background.
Intents
2 Implicit intents

Do not name a specific component, but instead declare a general action to


perform, which allows a component from another app to handle it

With implicit intent, the Android system finds the appropriate component to
start by comparing the contents of the intent to the intent filters declared in the
manifest file of other apps on the device.

For example, When you tap the share button in any app you can see the Gmail,
Bluetooth, WhatsApp, Telegram, and other sharing app options. Here user
sends a request is the implicit intent request which can be handled by these
Gmail, Bluetooth-like app.
Intents
How an implicit intent is delivered through the system to start another activity
Intents
How an implicit intent is delivered through the system to start another activity

1 Activity A creates an Intent with an action description and passes it to startActivity() method.

2 The Android System searches all apps for an intent filter that matches the intent. When a
match is found, then

3 The system starts the matching activity (Activity B) by invoking its onCreate() method and
passing it the Intent.
Intent-Filters
An intent filter is an expression in an app's manifest file that specifies the type of
intents that the component would like to receive.

For example, by declaring an intent filter for an activity, you make it possible for
other apps to directly start your activity by sending an implicit intent.

Likewise, if you do not declare any intent filters for an activity, then it can be started
only with an explicit intent.

Thus If the intent matches an intent filter, the system starts that component and
delivers it the Intent object. If multiple intent filters are compatible, the system
displays a dialog so the user can pick which app to use.
Broadcast
Android apps can send or receive broadcast messages from the Android system and
other Android apps.

These broadcasts are sent when an event of interest occurs.

For example, the Android system sends broadcasts when various system events
occur, such as when the system boots up or the device starts charging.

Apps can also send custom broadcasts, for example, to notify other apps of
something that they might be interested in (for example, some new data has been
downloaded).

The component of an Android app this is responsible for receiving such broadcasts
message is, called broadcast receivers.
Broadcast
Apps can register to receive specific broadcasts. When a broadcast is sent, the
system automatically routes broadcasts to apps that have subscribed to receive that
particular type of broadcast.

Although, broadcast receivers do not display a user interface, but in some cases
they may create a status bar notification to alert the user when a broadcast event
occurs

Types of broadcasts:
1 Normal Broadcasts

2 Ordered Broadcast
Types of Broadcasts
1 Normal Broadcasts:
They are asynchronous in nature.
Many receivers can be activated at the same time, but does not have to be in
defined order.
Still, they are very efficient.

2 Ordered Broadcasts:
Synchronous in nature.
Broadcast received by one receiver passes it to other receiver, in one to one and
sequential basis.
Either receiver will pass the result to another receiver, or it may completely
destroy the broadcast.
Widgets
Widgets are small application views embedded on the home screen providing quick
access to the app’s functionality and data.

A Widget is a simple application extension that is often part of a larger application


already installed on the device.

Widgets come in all shapes and sizes; are customizable and reside on the home
screen for quick access.

Widgets provide user with quick access to information without requiring to open the
app that manages the information.

For example, calendar widget provides quick view of upcoming calender events
without opening the calender.
Widgets
Types of Widgets:
1 Information Widgets

2 Collection Widgets

3 Control Widgets

4 Hybrid Widgets
Widgets
1 Information Widgets

Information widgets typically display a


few crucial information elements and
track how that information changes
over time.
Good examples for information
widgets are weather widgets, clock
widgets or sports score tracking
widgets.

Touching information widgets typically An information widget from a weather app


launches the associated app and opens
a detailed view of the widget
information.
Widgets
2 Collection Widgets

Collection widgets specialize in displaying multiple elements of the same type.

For example, collection of pictures from a gallery app, a collection of articles


from a news app or a collection of emails/messages from a communication
app.

Collection widgets can scroll vertically.


Widgets
3 Control Widgets
The main purpose of a control widget is to
display often-used functions, so that the user
can trigger right from the home screen without
having to open the app first.
You can think of them as remote controls for
an app.
An example of a control widget is a home
control widget that lets users turn lights on or
off in different rooms of a house.
Interacting with a control widget may or may
not open an associated detail view in the app.
This depends on whether the control widget's Example of a control widget
function outputs any data, such as in the case
of a search widget.
Widgets
4 Hybrid Widgets
Hybrid Widget combine elements of different types (information, collection, or
control)

Example of a hybrid widget(music app)

A music player widget is primarily a control widget, but also keeps the user
informed about what track is currently playing. It essentially combines a
control widget with elements of an information widget type.
Notifications
Notifications are the application alerts, that are used to draw user’s attention to
some particular app event, without stealing the focus/interrupting the current
activity of the user.

They are generally used to grab the user’s attention when the application is not
visible/active, particularly from within a Service, or Broadcast receiver.

For example, E-mail pop ups, messenger pop ups etc.


Other Additional components
These additional components are used in the construction of the above primary
components of an Android app, their logic & using between them.

1 View :- A view is the UI element such as button, label, text field etc. drawn on scree.

2 Fragment :- They are like parts of an activity. An activity can display one or more fragments
on the screen at the same time.

3 The manifest file (AndroidManifest.xml) :– Before the Android system can start an app
component, the system must know that the component exists, which is done by reading the
manifest file(where the app declares all of its components.)
References

R Reference for this topic

1 Book: Android application development for java programmers. By James C. Sheusi.


Publisher: Cengage Learning, 2013.

2 Web: https://en.wikipedia.org/wiki/Android_version_history

3 Web: https://www.tutorialspoint.com/android/android_overview.htm

4 Web: https://www.javatpoint.com/android-tutorial

5 Web: https://developer.android.com/guide/components/activities
References

R Reference for this topic


6 Web: https://developer.android.com/guide/components/fundamentals

7 Web: https://developer.android.com/reference/android/app/Activity

8 Web: https://developer.android.com/guide/components/services

9 Web: https://developer.android.com/guide/topics/providers/content-providers
9 Web: https://developer.android.com/guide/components/broadcasts

You might also like