Faculty of Computer Applications Bachelor of Computer Applications Semester - 5 Subject: Android Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

FACULTY OF COMPUTER APPLICATIONS

Bachelor of Computer Applications


Semester -5
Subject: Android Programming

Unit-2

Basics Of Android

[1] OHA

Open Handset Alliance

 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”

1 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

[2] Android Development Tools

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 −

Sr.No Tool & description


1 android

This tool lets you manage AVDs, projects, and the installed components of the SDK
2 ddms

This tool lets you debug Android applications


3 Draw 9-Patch

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

Shrinks, optimizes, and obfuscates your code by removing unused code


7 sqlite3

Lets you access the SQLite data files created and used by Android applications
8 traceview

Provides a graphical viewer for execution logs saved by your application


9 Adb

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with
an emulator instance or connected Android-powered device.

2 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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.

Some of the platform tools are listd below −

 Android Debug bridge (ADB)


 Android Interface definition language (AIDL)
 aapt, dexdump , and dex e.t.c

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.

[3] Android SDK

What Is the Android SDK?

3 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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.

How to Install the Android SDK

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.

4 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

 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:

 Android SDK Build-Tools


 Android Emulator
 Android SDK Platform-Tools
 Android SDK Tools
 Documentation for Android SDK

5 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

Click Apply, and Android Studio will install the selected tools and packages.

What Is the SDK Manager?

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.

6 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

What Are the Components of the Android SDK?

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

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.

SDK Build Tools

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.

7 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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:

 simulate phone calls and text messages


 simulate different network speeds
 specify the location of the device
 simulate hardware sensors such as rotation
 access Google Play Store and much more

Often it is faster and easier to test your app with an emulator instead of using a physical device.

[4] Building Blocks of Android

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.

8 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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 −

Sr.No Components & Description


1 Activities An activity is a class that represents a single screen. It is like a Frame in AWT. They
dictate the UI and handle the user interaction to the smart phone screen.
2 Services Service is a background process that can run for a long time. 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 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.

9 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

An activity is implemented as a subclass of Activity class as follows −

public class MainActivity extends Activity {


}

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.

A service is implemented as a subclass of Service class as follows −

public class MyService extends Service {


}

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.

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message is


broadcaster as an Intent object.

public class MyReceiver extends BroadcastReceiver {


public void onReceive(context,intent){}
}

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.

A content provider is implemented as a subclass of ContentProvider class and must implement a


standard set of APIs that enable other applications to perform transactions.

public class MyContentProvider extends ContentProvider {


public void onCreate(){}
}

We will go through these tags in detail while covering application components in individual chapters.

10 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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 −

S.No Components & Description


1 Fragments Fragments are like parts of activity. An activity can display one or more fragments
on the screen at the same time.Represents a portion of user interface in an Activity.
2 Views UI elements that are drawn on-screen including buttons, lists forms etc.
3 Layouts View hierarchies that control screen format and appearance of the views.
4 Intents Messages wiring components together.

Intent is used to invoke components. It is mainly used to:

Start the service

 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.

5 Resources External elements, such as strings, constants and drawable pictures.


6 Manifest Configuration files for the application. It contains information’s about activities,
content providers, permissions etc. It is like the web.xml file in Java EE.
7 Android Virtual Device(AVD) It is used to test the android application without the need for
mobile or tablet etc. It can be created in different configurations to emulate different types
of real devices.

Install 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.

Run an Android app on the Emulator

We can run an Android app form the Android Studio project, or we can run an app which is installed
on the Android Emulator as we run any app on a device.

To start the Android Emulator and run an application in our project:

1. In Android Studio, we need to create an Android Virtual Device (AVD) that the emulator can use
to install and run your app. To create a new AVD:-

11 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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.

12 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

1.4 Select the system image for the particular API level and click Next. This leads to open a Verify
Configuration page.

13 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

1.5 Change AVD properties if needed, and then click Finish.

2. In the toolbar, choose the AVD, which we want to run our app from the target device from the
drop-down menu.

3. Click Run.

Launch the Emulator without first running an app

To start the emulator:

1. Open the AVD Manager.


2. Double-click an AVD, or click Run

While the emulator is running, we can run the Android Studio project and select the emulator as the
target device. We can also drag an APKs file to install on an emulator, and then run them.

[5] Android Architecture

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

1. linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications

Let's see the android architecture first.

14 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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.

15 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

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.

[6] Android Activity Life Cycle

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android


Activity is the subclass of ContextThemeWrapper class.

An activity is the single screen in android. It is like window or frame of Java.

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.

16 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

Android Activity Lifecycle methods

Let's see the 7 lifecycle methods of android activity.

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.

17 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

[6] AndroidManifest.xml file

The AndroidManifest.xml file contains information of your package, including components of the
application such as activities, services, broadcast receivers, content providers etc.

It performs some other tasks also:

 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.

A simple AndroidManifest.xml file looks like this:

18 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

Elements of the AndroidManifest.xml file

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.

android:theme represents a common theme for all the android activities.

<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.

android:label represents a label i.e. displayed on the screen.

android:name represents a name for the activity class. It is required attribute.

<intent-filter>

intent-filter is the sub-element of activity that describes the type of intent to which activity, service
or broadcast receiver can respond to.

<action>

It adds an action for the intent-filter. The intent-filter must have at least one action element.

<category>

It adds a category name to an intent-filter.

19 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

[7] Anatomy of Android Application

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

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

View

A view is the UI element such as button, label, text field etc. Anything that you see is a view.

Intent

Intent is used to invoke components. It is mainly used to:

 Start the service


 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.

For example, you may write the following code to view the webpage.

1. Intent intent=new Intent(Intent.ACTION_VIEW);


2. intent.setData(Uri.parse("http://www.javatpoint.com"));
3. startActivity(intent);

Service

Service is a background process that can run for a long time.

There are two types of services local and remote. Local service is accessed from within the
application whereas remote service is accessed remotely from other applications running on the
same device.

20 Prepared By : Ravi Nimavat


FACULTY OF COMPUTER APPLICATIONS
Bachelor of Computer Applications
Semester -5
Subject: Android Programming

Content Provider

Content Providers are used to share data between the applications.

Fragment

Fragments are like parts of activity. An activity can display one or more fragments on the screen at
the same time.

AndroidManifest.xml

It contains information’s about activities, content providers, permissions etc. It is like the web.xml
file in Java EE.

Android Virtual Device (AVD)

It is used to test the android application without the need for mobile or tablet etc. It can be created
in different configurations to emulate different types of real devices.

[8] Building a Sample Android App

Step 1: Create a new project

1. Open Android Studio.


2. In the Welcome to Android Studio dialog, click Start a new Android Studio project.
3. Select Basic Activity (not the default). ...
4. Give your application a name such as My First App.
5. Make sure the Language is set to Java.
6. Leave the defaults for the other fields.
7. Click Finish.

21 Prepared By : Ravi Nimavat

You might also like