Unit -I Notes
Unit -I Notes
• Introducing Android :
Android Inc was founded in Palo Alto of California, US by Andy Rubin, Rich Miner, Nick
Scars ant Chris White during 2003. After that it was acquired by Google in 2005.
• What is Android ?
1. Android is a stack of software for mobile devices that are an Operating System, Middleware
and Key Applications.
Operating
System
Middleware
Key
Application
Handset
Manufactures
Commercialization
Software
Companies
companies
• Android Ecosystem :
For Android and develop Android Applications, it is very important to understand the
Entire ecosystem of Android and the stakeholders of this ecosystem :
Google
Original
Consumers Equipment
Manufacturer
(OEM)
Android
Application
Development
Freelancers Centers
Companies
Fig. 1.4 Android Ecosystem
The stakeholders are the Consumers that own Android Devices. But there are others as well :
• Google : It develops Android.
• OEMs (Original Equipment Manufactures) : They manufacture the hardware and as
well the custom applications computers. Example : LG, Sony, HTC, Samsung, etc.
• Application Development Companies : They are the major contributors to the echo
system and employ Android developers, and also contract out the product development
to services companies. Example : WhatsApp, Facebook, Angry Birds Gaming App, etc.
• Freelance Android Developers : Developers have the skill set to development they are
to create their own application and published them on to Google play store, freelancers
can also generates money by developing applications for product companies.
• Describing Android Architecture :
The architecture of an Android system consists of a software stack of different
layers. Each layer contains group of various program components. The software stack of
architecture works in a way that each layer provides services to the layer just above it.
Together, it includes OS, middleware, and applications. Putting all layers together comprises
and architecture called Android Architecture.
Applications
Application Framework
Linux Kernel
Brinder (IPS)
Display Driver Camera Driver Flash Memory Driver Driver
Power
Keypad Driver WiFi Driver Audio Drivers
Management
1. Application Layer
2. Application Framework Layer
3. Android Runtime
4. Libraries
5. Linux Kernal
As the Android Architecture is a software stack of different layers. Let’s discuss the each layer
one-by-one.
5. Linux Kernal : Linux Kernal is the root layer in the android architecture. The interaction of
the hardware into software is provided by means of Linux. All the hardware devices are handled
by this Kernal.
Drivers are the small program written specifically to communicate with the hardware.
It has all the major hardware drivers a000s display driver, camera driver, power management
driver, and so forth.
For example, the Linux kernel has camera driver. On installing the Android OS in a compatible
device, the Linux kernel will search the camera a then make it available to the system by using
camera driver.
The Linux kernel not only contains hardware drivers, but also serves as the abstraction
layer for other layers Since the core of the Android system lies in the Linux kernel, all the main
functionality of the system is based of Linux. The foundation of Android is Linux, which is a
much powerful system; therefore, it provides a great versatility to the Android OS to work it on
multiple devices without any (or few) glitches.
4. Libraries Layer : The layer next to the Linux kernel layer is the Android's native libraries. 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 for fast procedural transactions
of data. A brief description of some of the native Android libraries is as follows :
• Media library provides support to play and record an audio and video formats.
• Surface manager responsible for managing access to the display subsystem.
• SGL and OpenGL both cross-language, cross-platform application program interface
(API) are used for 2D and 3D computer graphics.
• SQLite provides database support and FreeType provides font support.
• Web-Kit : This open source web browser engine provides all the functionality to display
web content and to simplify page loading.
• SSL (Secure Sockets Layer) is security technology to establish an encrypted link
between a web server and a web browser.
a. Dalvik Virtual Machine (DVM) : The Dalvik Virtual Machine (DVM) is an android virtual
machine optimizes for mobile devices. It optimized the virtual machine for memory,
battery life and performance.
Dalvik is a name of town in Iland. The Dalvik VM was written by “Dan Bornstein”.
Fig. 1.5 Dalvik Virtual Machine
• Optimizing the Virtual Machine for memory, battery life, and performance.
• Conversion of class files into .dex file through Dex compiler that runs on Dalvik VM.
• Converting multiple class files into dex files.
The Dex compiler helps convert the class file into .dex file, the following image shows how it
flows:
• First of all the .java file converts into .class file with the help of Java compiler.
• Next .class file converts into .dex file using Dec compiler.
• Then finally the packaging process is handled by the Android Assets packaging (aapt)
tools.
b. Core Libraries :
The core libraries enable android developers to write android application using standard
java programming language.
Core Java Libraries are the exclusively design java libraries for android rather than java SE
(Standard Edition) and java ME (Mobile Edition). However most of the functionality of this
libraries similar to the SE libraries.
This libraries consist of data structure, file access, network access, utilities and Graphics.
2. Android Runtime :
Application framework is of utmost importance for developer as the blocks inside this layer
directly communicate with Android applications.
The basic and much needed functionalities such as resource management and voice call
management are provided by this layer.
From the developer's perspective, this layer provides basic building blocks for Android
applications. The important blocks of Application framework are as follows :
1. Application Layer :
Application layer is the top layer in the Android architecture. This is the place where our
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.
Pre-installed applications can be replaced by newly developed applications. This is the fact
which makes Android OS so flexible. Many device vendors modify the pure Android applications
with their own applications for the sake of different feel and look.
• Android API :
API stands for application programming interface, which, as a portion of code, acts as an
interface for communication among different software. An API contains specifications to develop
a program by means of all the building blocks. These building blocks are then joined by a
programmer.
The applications developed for Android are also called Android apps. Many new features
have been added for users and application developers. Let's discuss the most noteworthy and
useful new APIs for developers. API levels available up to Android version 12 are tabulated in
Table 1.1 :
Table 1.1 Description of the Android API Levels
Version API Level
1.5 3
1.6 4
2.1 7
2.2 8
2.3 – 2.3.2 9
2.3.3 – 2.3.7 10
3.1 12
3.2 13
4.0 14
4.1 16
4.2 17
4.3 18
4.4 19
5.0 21
5.1 22
7.0 24
7.1 25
8.0 26
8.1 27
12 31
1. API level provides the API review offered by a version of the Android platform.
2. The platform provides a framework API, which allows the applications to communicate with
the underlying Android system.
3. The API level for Android consists of packages, classes, Extensible Markup Language (XML)
element and attributes intents, and a set of permissions.
4. The API designers always keep in the mind the backward compatibility with the lower API
levels. For examples - the API level 14 has compatibility with the API level 3.
• Features of Android :
Android, the mobile operating system developed by Google, offers a wide range of
features that have evolved over time. Here are some of the key features of Android :
1. User Interface Customization : Android provides extensive options for customizing the
user interface, including widgets, wallpapers, themes, and icon packs.
2. Multitasking : Users can easily switch between multiple apps and perform tasks
simultaneously using Android's multitasking capabilities, such as split-screen mode and
picture-in-picture mode.
4. Google Assistant : Google's virtual assistant, Google Assistant, is deeply integrated into
Android, providing voice-controlled access to various functions, information, and
services.
5. Google Play Store : Android users have access to a vast ecosystem of apps, games,
movies, books, and music through the Google Play Store, which serves as the primary
marketplace for downloading and purchasing digital content.
6. Security : Android offers various security features, including built-in malware protection,
app sandboxing, secure boot process, and regular security updates provided by Google.
8. Integration with Google Services : Android seamlessly integrates with various Google
services, such as Gmail, Google Maps, Google Drive, and Google Photos, providing a
cohesive ecosystem for users.
9. Cloud Storage : Android devices often come with built-in support for cloud storage
services, enabling users to back up their data, sync files across devices, and access their
content from anywhere.
10. Accessibility : Android offers a wide range of accessibility features, including screen
readers, magnification gestures, captioning, and voice commands, to make the platform
more inclusive for users with disabilities.
These are just some of the key features of Android, and the platform continues to evolve with
each new version and update, incorporating new functionalities and improvements.
• Discussing About Android Application :
1. Android applications are developed in Java. Although the full blend of Java is not used, it
can be considered as a part of Java.
2. The Java part used in Android application development does not include the classes that
are not important for mobile devices.
3. XML and Apache Ant are used alongside Java in Android development.
4. Proficiency in Java and XML basics prepares you for Android app creation.
Now, Let's discuss the basic components used for application development.
1. Activity :
It is obvious that an application can have multiple activities which can be loosely coupled.
The main or the very first screen (as soon as you launch an application) of the application is known
as Main Activity. When you switch for the next activity (screen), the previous activity is halted and
stored in a stack called as back-stack. As stack is a data structure with the last-in-first-out property,
on pressing the Back button by user, the previous activity is activated. Consequently, the current
activity (from where the Back button was press demolished as it was popped out from back stack.
Whenever we introduce new screen in our Android Project, then two files are created one is XML
file and another one is java file.
The java code of the Activity is –
public class MainActivity extends AppCompactActivity {
}
For example – Image application can be started in an email application to compose an email that
shares an image. The picture below depicts how each new activity adds an item to back stack and
how the current activity is destroyed and previous activity is resumed.
2. Services :
An application component in the working process that does not use UI is called a service.
For example, a service that is playing music, providing contents, etc., from the background but
not appearing on the screen. A service is an application component that can perform long-
running operations in the background and does not provide a UI. Even on switching to another
application, the services of the previously activated apps not cannot be stopped.
The java code of the Services is –
public class My_Services extends Service {
}
For example - We can surf the internet or use any other application while listening to music.
3. Content Providers :
Accessing of contents from the central data warehouse is managed by a content provider. Being
a part of Android application, the content provider empowers its UI for working with the data.
The java code of the Services is –
public class My_Content_Provider extends Content Provider {
public void onCreate() {
}
}
For example - We can consider looking for contact details in contact list. Or we might want
photos from the gallery which are also provided by Content Provider.
4. Broadcast Receivers :
Many system-generated notifications, such as battery low, downloading starts, or file received,
are broadcasted by broadcast receivers. However, some applications can also stimulate
broadcast receivers. For example, an application informs the other application that data has
been downloaded for them to use. The broadcast receivers do not have a UI; however, they send
a notification to the status bar to alert the user. In essence, a broadcast receiver is a component
that alerts a user by sending a notification, which is intended to do a very little work (just notify).
It just acts as a gateway for other components.
The java code of the Broadcast Receivers is –
}
}
For example - Notify the user that the battery is low. Android developers can use broadcast
messages in the application or outside the normal flow.
• The manifest File :
1. The manifest file is the core of Android development.
2. It is an XML file and is so important to Android application that sometimes it is also known
as the heart of the Android application. It is named as AndroidManifest.xml file.
3. AndroidManifest.xml file contain information of various packages including component of
the application such as Activity, Services, Content Provider and Broadcast Receiver.
4. It performs another task also :
i. It is responsible to protect application to access any protected part providing
the permissions.
ii. It also declares the android API that the application is going to use.
iii. It lists the instrumentation classes. The instrumentation classes provide
profiling and other information. This information is removed just before the
application is published etc.
5. The elements used in the above XML file are described below.
<manifest>
manifest is the root element of the AndroidManifest.xml file. It has package attribute that
describes the package name of the activity class.
6. The commonly used attributes are of this element are icon, label, theme etc.
3. Eclipse IDE
Once the download is complete, locate the downloaded file and double-
click on it to run the installer.
3. Choose Setup Type:
The installer will prompt us to choose the type of setup we want. We can
choose the Standard setup, which includes the most commonly used
features, or the Custom setup, which allows us to customize the
installation further.
4. Select Components:
Select the installation location for Android Studio. The default location is
usually fine, but we can choose a different location if we prefer.
Specify the Start Menu folder where shortcuts for Android Studio will be
created.
7. Install:
Click on the "Install" button to begin the installation process. The installer
will now download and install the selected components.
On the first launch, Android Studio will prompt us to import settings from
a previous version (if any) and to customize the setup according to our
preferences.
10. Download SDK Components :
After completing the setup, we can create a new Android project or open
an existing one to start developing our app.
After installing Android Studio, launch it. Android Studio will prompt us to
download the necessary SDK components if they're not already installed. For this, we have
to follow the on-screen instructions to download and install the required SDK
components.
Android Virtual Device (AVD) allows us to test our apps on virtual devices. Open
Android Studio, go to "Tools" > "AVD Manager," and create a new virtual device by
following the prompts. Choose a device definition and system image that we want to
emulate.
We might need to set up environment variables for Java and Android SDK if they
are not automatically detected. This step ensures that other tools can locate the
JDK and Android SDK. Here's how to set environment variables :
Regularly update Android Studio and SDK components to access the latest
features and improvements. You can check for updates within Android Studio via
"Help" > "Check for Updates" and update SDK components through the SDK
Manager.
So, this is the environment setup we have to follow for Android Programming.
That's the process of Android Virtual Device with Android Studio ! We have now set up
the Android SDK and configured an Android Virtual Device. We can use this virtual
device for testing our Android applications without needing a physical device.
• Setting Up Android Physical Device :
Setting up an Android physical device for development with USB debugging involves a
few steps to enable developer options and USB debugging on the device itself, as well
as configuring our development environment to recognize the device. Here's a step-by-
step guide :
adb devices
● If our device is listed, you're all set! You can now use it for development
and debugging.
Note :
2. If our device is not recognized after following these steps, try restarting both our
computer and our Android device, and make sure we have installed any necessary
drivers.
The Java perspective in Eclipse lets us access the associated Java libraries, which
are to be used with Android applications in Eclipse IDE. The sections of Java perspective
in Eclipse IDE are as follows :
1. Package Explorer :
● The Package Explorer provides a hierarchical view of the files and folders in our
Eclipse project.
● It organizes our project's resources, including Java source files, XML files, images,
and other assets, into a navigable tree structure.
● We can expand and collapse folders to navigate through our project's directory
structure, making it easy to locate and manage files.
● It allows us to perform various operations on project resources, such as creating,
deleting, renaming, copying, and moving files and folders.
2. Task List :
● The Task List displays a list of tasks, comments, and other markers that are
associated with our project's source code files.
● Tasks can include TODO comments and custom task tags that we define.
● It helps us keep track of pending work items, reminders, and areas of code that
need attention.
● We can navigate directly to the location of a task in our code by double-clicking
on it in the Task List.
● The Java Class Pane, also known as the Editor area, is where we edit and view the
contents of Java class files.
● It provides a text editor with syntax highlighting, code completion, and other
features to assist us in writing Java code.
● We can open and switch between multiple Java class files in separate tabs within
the Java Class Pane.
● It offers tools for navigating through our code, such as the Outline view and
Quick Outline feature.
4. Outline:
● The Outline view displays a structured outline of the current Java class file being
edited in the Java Class Pane.
● It provides a high-level overview of the class's structure, including its methods,
fields, inner classes, and other members.
● We can quickly navigate to specific sections of the class by clicking on entries in
the Outline view.
● It allows us to collapse or expand sections of the class to focus on specific parts
of the code.
The Java perspective in Eclipse provides a set of essential tools and views for Java
development, including the Package Explorer for managing project resources, the Task
List for tracking tasks and markers, the Java Class Pane for editing and viewing Java class
files, and the Outline view for navigating class structures. These components work
together to support efficient and productive Java development in Eclipse.
DDMS stands for Dalvik Debug Monitor Server. It's a tool provided by the Android
SDK (Software Development Kit) that allows developers to debug Android applications
and analyze the behavior of applications running on Android devices or emulators. Below
are the main components of the DDMS perspective :
1. Devices :
● The Devices view lists all connected Android devices and running
emulators.
● It provides information about each device/emulator, including its serial
number, state, and other properties.
● We can select a device/emulator to view detailed information and perform
various debugging and monitoring tasks.
2. Emulator Control:
● The Emulator Control view allows us to interact with and control Android
emulators.
● We can simulate phone calls, SMS messages, and location updates, as well
as control the orientation and battery level of the emulator.
● This feature is particularly useful for testing and debugging applications
that rely on phone calls, messaging, or location services.
3. LogCat :
● The LogCat view displays real-time system log messages from connected
Android devices and emulators.
● It provides filtering options to focus on specific log messages based on
priority level, tag, or text.
● LogCat is essential for debugging and diagnosing issues in Android
applications, as it allows developers to monitor runtime behavior and trace
execution flow.
4. Threads:
● The Threads view provides information about the threads running in the
selected Android application.
● It displays the thread's name, state, priority, and stack trace, allowing
developers to analyze multithreaded behavior and identify potential issues
such as deadlocks or race conditions.
5. Heap:
● The Heap view offers insights into the memory usage of the selected
Android application.
● It shows a graphical representation of the application's heap memory,
including the size of allocated objects and the distribution of memory
across different object types.
● Developers can use the Heap view to identify memory leaks, optimize
memory usage, and troubleshoot performance issues related to memory
management.
6. Allocation Tracker :
● The Allocation Tracker view allows developers to monitor object
allocations and deallocations in the selected Android application.
● It provides detailed information about each allocation event, including the
type of object, allocation size, allocation stack trace, and the number of
references.
● Developers can use the Allocation Tracker to analyze memory usage
patterns, detect memory leaks, and optimize resource allocation in their
applications.
7. File Explorer :
● The File Explorer view enables browsing and managing files on the file
system of connected Android devices and emulators.
● Developers can navigate through the device's directory structure, upload
and download files, and perform file operations such as copy, paste,
delete, and rename.
● This feature is useful for inspecting application files, transferring data
between the device and the computer, and debugging file-related issues.
To enable the DDMS perspective in Eclipse, we can follow the steps : Select "Window"
→ "Open Perspective" → "DDMS" option, or select "Window" → "Open Perspective"
→ "Other" option → "DDMS" option. This will switch the Eclipse IDE to the DDMS
perspective, where we can access the mentioned tools and views for debugging and
monitoring Android applications.
We have seen that whenever we learn new programming languages, the first
program always displays ‘hello world’ on the screen. The same trend continues with
Android here; yet, we will write a somewhat comprehensible "hello world" application. It
is very easy and practically no coding is required. However, to make this application
interesting, we will add some extra coding in it.
5. Edit MainActivity.java :
● Inside MainActivity.java, you'll see a method named onCreate. This method
is called when the activity is created.
● Within onCreate, locate the line setContentView(R.layout.activity_main);.
This line sets the layout for the activity.
● We'll edit the activity_main.xml layout file next to create the UI for our
"Hello World" message.
By following these steps, we can create and run a "Hello World" application using
Eclipse IDE for Android development.
● Running Your Application :
The steps to run the "Hello World" application (MyFirstApp) on the Android Emulator
as described :
1. Open Eclipse IDE and navigate to any directory containing the MyFirstApp
project in the Package Explorer.
● Click the "Run" icon in the toolbar of Eclipse IDE.
● Alternatively, select the application and click on the "Run" option in the
menu bar. We can also choose "Run As" -> "Android Application."
2. The Android emulator (AVD) will appear as the next window. It might take some
time to boot up, and then the home screen will be displayed.
3. Click the Applications Icon on the home screen of the emulator. Look for the
‘MyFirstApp’ application icon.
4. Click on the icon of the ‘MyFirstApp’ application. The screen will display the
output, which should be "Hello world!"
By following these steps, we can create and run the "Hello World" application on an
Android emulator.
The purpose of exploring the application is to know how the “hello world” text
message has been displayed by the “MyFirstApp” application.
To explore the internal structure of the “MyFirstApp” application and understand how
the "Hello world" text message is displayed, we need to familiarize ourselves with some
important directories within the application.
1. AndroidManifest.xml :
The importance of the AndroidManifest.xml file and its components in the MyFirstApp
application, we have :
Components of AndroidManifest.xml:
● <manifest> :
● It's the main element enclosing the entire structure of the manifest file
and acts as the root element.
● <uses-sdk>:
● This element indicates the compatibility of the application.
● It specifies the minimum and maximum version (API level) of the
Android application platform required to run the app.
● <application>:
● This element is a significant part of the manifest file.
● It contains declarations of various components and attributes related
to the application.
● <activity>:
● Declared inside the <application> element.
● Responsible for defining the visual UI of the application.
● Each activity represents a screen or a user interface component within
the application.
● <intent-filter>:
● This declaration is used for activities, services, and broadcast receivers to
specify how they respond to incoming messages called intents.
● It defines the criteria for an intent to match with the component.
● Activities, services, and broadcast receivers can have one or more intent
filters to specify the types of intents they can handle.
● <action> :
● This element is used within an <intent-filter> to specify the action(s) that
the component can handle.
● Actions describe the overall behavior or purpose of the intent.
● Examples of actions include "android.intent.action.VIEW" for viewing data
and "android.intent.action.SEND" for sending data.
● <category>:
● This element is also used within an <intent-filter> to specify additional
categorization for the intent.
● Categories provide more specific information about the intent's purpose or
behavior.
● Common categories include "android.intent.category.DEFAULT" for the
default category and "android.intent.category.LAUNCHER" for activities that
can be the entry point of the application.
By understanding and modifying the AndroidManifest.xml file, developers can control the
behavior and characteristics of their Android applications, including compatibility,
permissions, and declarations of various components like activities, services, broadcast
receivers, and content providers.
2. MainActivity.java :
Package Declaration:
● The first line represents the package declaration for the MainActivity.java
file.
● The package name is com.kogent.android.myfirstapp, which identifies the
location of the file within the project's directory structure.
Import Statements:
● The next lines import necessary classes required for the application.
● For example, android.app.Activity and android.os.Bundle are imported to
utilize functionalities provided by the Android framework.
In summary, the MainActivity.java file contains the main code for the MainActivity class,
which is responsible for defining the behavior and appearance of the main activity in the
MyFirstApp application. The onCreate() method is overridden to initialize the activity and
set its layout using the activity_main.xml file.
3. activity_main.xml :
The process of viewing and understanding the code in the activity_main.xml file:
Open activity_main.xml:
● Navigate to the res directory in your project.
● Inside the res directory, locate the layout subdirectory.
● Find the activity_main.xml file within the layout directory.
In summary, by following these steps, you can access and examine the XML code of the
activity_main.xml file, which defines the layout of the main activity's user interface in the
MyFirstApp application. This file is crucial for understanding how the UI works and how
widgets are managed within the application.
4. strings.xml :
In summary, the strings.xml file contains string resources used within the Android
application. It allows for easy management and customization of text displayed in the user
interface and other parts of the application. By accessing and modifying this file,
developers can control the text displayed in their Android application, including the "Hello
world!" message.
● The Command-Line Tools :
To utilize command-line tools for Android application development, we need to have the
Android SDK installed on our system. The Android SDK contains the necessary tools,
libraries, and APIs required for building Android applications. Additionally, we need to
have the applicable API levels installed, depending on the version of Android we are
targeting for our application.
By using command-line tools, developers can perform various tasks such as creating
projects, compiling code, managing dependencies, and running tests directly from the
terminal or Command Prompt, without relying on graphical user interfaces provided by
IDEs. This approach is preferred by some developers for its simplicity, speed, and
flexibility.
Let's first set the environment to work with command-line tools.
The steps to set up the environment variable to access the SDK are as follows:
5. Confirm Changes:
● Click "OK" to save the changes and close the Edit User Variable dialog box.
● Click "OK" again to close the Environment Variables dialog box.
● Finally, click "OK" to close the System Properties dialog box.
These steps ensure that the environment variable is correctly configured to allow access
to the Android SDK tools and platform-tools from the command line. This configuration
is necessary for performing various development tasks using command-line tools for
Android development.
Creating a Project :
For creating a project using command-line tools with the Android SDK :
cd path\to\desired\directory
3. List Available Android Platforms:
● Execute the command android list targets.
● This command displays the existing Android platforms along with their
target IDs.
● Note down the target ID corresponding to the platform we want to
develop for.
5. Execute Command:
● Press Enter after entering the command to execute it.
Following these steps, our Android project will be set up and ready for development
using command-line tools. We can now proceed with further development tasks and
coding for our Android application.
● Using Command-Line :
Instructions for running an Android project using command-line tools:
These steps outline the process of building, installing, and running an Android project
using command-line tools and Apache Ant. This approach provides developers with a
streamlined method for managing and deploying Android applications without relying
on IDEs.