Faculty of Computer Applications Bachelor of Computer Applications Semester - 5 Subject: Android Programming
Faculty of Computer Applications Bachelor of Computer Applications Semester - 5 Subject: Android Programming
Faculty of Computer Applications Bachelor of Computer Applications Semester - 5 Subject: Android Programming
Unit-2
Basics Of Android
[1] OHA
The Open Handset Alliance (OHA) is a collection of more than 84 technology companies,
including hardware manufacturers, mobile carriers, and software developers. Of particular
note are the prominent mobile technology companies Motorola, HTC, T-Mobile, and
Qualcomm.
It was established on 5th November, 2007, led by Google.
In their own words, the OHA represents the following:
“The OHA hopes to deliver a better mobile software experience for consumers by providing
the platform needed for innovative mobile development at a faster rate and with higher quality than
existing platforms, without licensing fees for either software developers or handset manufacturers”
The android developer tools let you create interactive and powerful application for android
platform. The tools can be generally categorized into two types.
SDK tools
Platform tools
SDK tools
SDK tools are generally platform independent and are required no matter which android platform
you are working on. When you install the Android SDK into your system, these tools get
automatically installed. The list of SDK tools has been given below −
This tool lets you manage AVDs, projects, and the installed components of the SDK
2 ddms
This tool allows you to easily create a NinePatch graphic using a WYSIWYG editor
4 emulator
This tools let you test your applications without using a physical device
5 mksdcard
Helps you create a disk image (external sdcard storage) that you can use with the emulator
6 proguard
Lets you access the SQLite data files created and used by Android applications
8 traceview
Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with
an emulator instance or connected Android-powered device.
Platform tools
The platform tools are customized to support the features of the latest android platform.
The platform tools are typically updated every time you install a new SDK platform. Each update of
the platform tools is backward compatible with older platforms.
The Android SDK includes a virtual mobile device emulator that runs on your computer. The
emulator lets you prototype, develop and test Android applications without using a physical device.
The Android SDK is a collection of software development tools and libraries required to develop
Android applications. Every time Google releases a new version of Android or an update, a
corresponding SDK is also released which developers must download and install. It is worth noting
that you can also download and use the Android SDK independently of Android Studio, but typically
you'll be working through Android Studio for any Android development.
The Android SDK comprises all the tools necessary to code programs from scratch and even test
them. These tools provide a smooth flow of the development process from developing and
debugging, through to packaging.
The Android SDK is compatible with Windows, macOS, and Linux, so you can develop on any of those
platforms.
The Android SDK is optimized for Android Studio, and hence to effectively reap its benefits, you will
need to install Android Studio. Having the Android SDK managed from within Android Studio is
easier since support for languages like Java, Kotlin, and C++ is handled automatically. Not only that,
but updates to the Android SDK are handled automatically by Android Studio.
To install the Android SDK from within Android Studio, first start Android Studio.
From the Android Studio start page, select Configure > SDK Manager.
If you already have Android Studio open, the SDK Manager icon is found on the top right
corner, as shown below.
Install the required Android SDK platform packages and developer tools. A good start is to install:
Click Apply, and Android Studio will install the selected tools and packages.
The Android SDK is composed of modular packages that you can download, install, and update
separately using the Android SDK Manager. The SDK Manager helps to update new SDK releases
and updates whenever a new Android platform is released. The SDK manager can be found in the
top-right corner of the Android Studio screen, as shown below.
All that is required to follow the instructions provided, and the updates will be immediately
downloaded to your environment.
The Android SDK consists of an emulator, development tools, sample projects with source code, and
the required libraries to build Android applications. Let's look at the key components one by one.
Android SDK Tools is a component of the Android SDK. It includes a complete set of development
and debugging tools for Android, and is included with Android Studio. The SDK Tools also consist of
testing tools and other utilities required to develop an app.
Build tools are required for building components for building the actual binaries for your Android
app. Always ensure your build tools component is up to date by downloading the latest version in
the Android SDK Manager.
SDK Platform-Tools
Android Platform-Tools are used to support the features for the current Android platform and are
necessary for Android app development. These tools interface with the Android platform on the
device you use for testing. They include:
Android Debug Bridge (adb): This is a handy command-line tool that lets you communicate
with a device. The adb command allows you to perform device actions, such as installing and
debugging apps. It also provides access to a Unix shell that you can use to run a variety of
commands on a device.
fastboot: This lets you flash a device with a new system image.
systrace: This tool helps collect and inspect timing information across all processes running
on your device at the system level. It's crucial for debugging app performance.
SDK Platform-Tools are backward compatible, so you need only one version of the SDK Platform-
Tools.
SDK Platform
For each version of Android, there's one SDK Platform available. These are numbered according to
the Android version (e.g. Android 7 Nougat) and an API version (e.g. API Level 24). Before you build
an Android app, you must specify an SDK Platform as your build target. Newer SDK Platform versions
have more features for developers, but older devices may not be compatible with the newer
platform versions.
Google APIs
Google provides a number of exclusive Google APIs to make developing your app easier. They also
offer a system image for the emulator so you can test your app using the Google APIs.
Android Emulator
The Android Emulator is a QEMU-based device-emulation tool that simulates Android devices on
your computer, allowing developers to test applications on different devices and Android API levels,
without needing to have physical devices for each. The emulator comes with configurations for
various Android phones, tablets, Wear OS, and Android TV devices.
The Android emulator provides almost all of the capabilities of a real Android device. You can
perform the following activities:
Often it is faster and easier to test your app with an emulator instead of using a physical device.
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 AndroidManifest.xml.
Application components are the essential building blocks of an Android application. These
components are loosely coupled by the application manifest file AndroidManifest.xml that describes
each component of the application and how they interact.
There are following four main components that can be used within an Android application −
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.
3 View A view is the UI element such as button, label, text field etc. Anything that you see is a
view.
4 Content Providers Content Providers are used to share data between the applications.They
handle data and database management issues.
Activities
An activity represents a single screen with a user interface,in-short Activity performs actions on the
screen. For example, an email application might have one activity that shows a list of new emails,
another activity to compose an email, and another activity for reading emails. If an application has
more than one activity, then one of them should be marked as the activity that is presented when
the application is launched.
Services
A service is a component that runs in the background to perform long-running operations. For
example, a service might play music in the background while the user is in a different application, or
it might fetch data over the network without blocking user interaction with an activity.
Broadcast Receivers
Broadcast Receivers simply respond to broadcast messages from other applications or from the
system. For example, applications can also initiate broadcasts to let other applications know that
some data has been downloaded to the device and is available for them to use, so this is broadcast
receiver who will intercept this communication and will initiate appropriate action.
Content Providers
A content provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. The data may be stored in the file
system, the database or somewhere else entirely.
We will go through these tags in detail while covering application components in individual chapters.
Additional Components
There are additional components which will be used in the construction of above mentioned
entities, their logic, and wiring between them. These components are −
Launch an activity
Display a web page
Display a list of contacts
Broadcast a message
Dial a phone call etc.
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.
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.
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.
1.4 Select the system image for the particular API level and click Next. This leads to open a Verify
Configuration page.
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.
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.
1. linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications
1) 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 and
resource access.
2) Native Libraries
On the top of linux kernel, their are Native libraries s uch as WebKit, OpenGL, FreeType, SQLite,
Media, C runtime library (libc) etc.
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) Android Framework
On the top of Native libraries and android runtime, there is android 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.
5) Applications
On the top of android framework, there are applications. All applications such as home, contact,
settings, games, browsers are using android framework that uses android runtime and libraries.
Android runtime and native libraries are using linux kernal.
By the help of activity, you can place all your UI components or widgets in a single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Method Description
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user.
onResume called when activity will start interacting with the user.
onPause called when activity is not visible to the user.
onStop called when activity is no longer visible to the user.
onRestart called after your activity is stopped, prior to start.
onDestroy called before the activity is destroyed.
The AndroidManifest.xml file contains information of your package, including components of the
application such as activities, services, broadcast receivers, content providers etc.
It is responsible to protect the application to access any protected parts by providing the
permissions.
It also declares the android api that the application is going to use.
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.
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.
<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.
<activity>
activity is the subelement of application and represents an activity that must be defined in the
AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
<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>
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 AndroidManifest.xml.
Activity
View
A view is the UI element such as button, label, text field etc. Anything that you see is a view.
Intent
For example, you may write the following code to view the webpage.
Service
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
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 information’s about activities, content providers, permissions etc. It is like the web.xml
file in Java EE.
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.