Unit 4 Android
Unit 4 Android
Introduction
Android is a complete set of software for mobile devices such as tablet
computers, notebooks, smartphones, electronic book readers, set-top boxes
etc.
It contains a linux-based Operating System, middleware and key mobile
applications.
It can be thought of as a mobile operating system. But it is not limited to
mobile only. It is currently used in various devices such as mobiles, tablets,
televisions etc.
What is Android?
Android is a software package and linux based operating system for mobile
devices such as tablet computers and smartphones.
It is developed by Google and later the OHA (Open Handset Alliance). Java
language is mainly used to write the android code even though other
languages can be used.
The goal of android project is to create a successful real-world product that
improves the mobile experience for end users.
There are many code names of android such as Lollipop, Kitkat, Jelly Bean, Ice
cream Sandwich, Froyo, Ecliar, Donut etc
Features of Android
After learning what is android, let's see the features of android. The important
features of android are given below:
1) It is open-source.
2) Anyone can customize the Android Platform.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It provides many interesting features like weather details, opening screen,
live RSS (Really Simple Syndication) feeds etc.
It provides support for messaging services(SMS and MMS), web browser,
storage (SQLite), connectivity (GSM, CDMA, Blue Tooth, Wi-Fi etc.), media,
handset layout etc.
Categories of Android applications
There are many android applications in the market. The top categories are:
o Entertainment
o Tools
o Communication
o Productivity
o Personalization
2
1.5 Cupcake 3
1.6 Donut 4
2.1 Eclair 7
2.2 Froyo 8
4.4 KitKat 19
5.0 Lollipop 21
6.0 Marshmallow 23
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
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 such as WebKit, OpenGL,
FreeType, SQLite, Media, C runtime library (libc) etc.Hello Java Program for
Beginners
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.
5
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.
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.
5. Content Provider
Content Providers are used to share data between the applications.
6. Fragment
Fragments are like parts of activity. An activity can display one or more
fragments on the screen at the same time.
7. AndroidManifest.xml
It contains informations about activities, content providers, permissions etc. It
is like the web.xml file in Java EE.
8. 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.
Android Emulator
The Android emulator is an Android Virtual Device (AVD), which represents
a specific Android device. We can use the Android emulator as a target device
to execute and test our Android application on our PC. The Android emulator
provides almost all the functionality of a real device. We can get the incoming
phone calls and text messages. It also gives the location of the device and
simulates different network speeds. Android emulator simulates rotation and
other hardware sensors. It accesses the Google Play store, and much more
Testing Android applications on emulator are sometimes faster and easier
than doing on a real device. For example, we can transfer data faster to the
emulator than to a real device connected through USB.
The Android emulator comes with predefined configurations for several
Android phones, Wear OS, tablet, Android TV devices.
Requirement and recommendations
The Android emulator takes additional requirements beyond the basic system
requirement for Android Studio. These requirements are given below:
o SDK Tools 26.1.1 or higher
o 64-bit processor
o Windows: CPU with UG (unrestricted guest) support
o HAXM 6.2.1 or later (recommended HAXM 7.2.0 or later)
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
7
Android Widgets
There are given a lot of android widgets with simplified examples such as
Button, EditText, AutoCompleteTextView, ToggleButton, DatePicker,
TimePicker, ProgressBar etc.
Android widgets are easy to learn. The widely used android widgets with
examples are given below:
Android Button
Let's learn how to perform event handling on button click.
Android Toast
Displays information for the short duration of time.00:00/11:48
Custom Toast
We are able to customize the toast, such as we can display image on the toast
ToggleButton
It has two states ON/OFF.
CheckBox
Let's see the application of simple food ordering.
AlertDialog
AlertDialog displays a alert dialog containing the message with OK and Cancel
buttons.
Spinner
Spinner displays the multiple options, but only one can be selected at a time.
AutoCompleteTextView
Let's see the simple example of AutoCompleteTextView.
RatingBar
RatingBar displays the rating bar.
DatePicker
Datepicker displays the datepicker dialog that can be used to pick the date.
TimePicker
TimePicker displays the timepicker dialog that can be used to pick the time.
ProgressBar
ProgressBar displays progress task.
8
Method Description
onResume called when activity will start interacting with the user.
Android Intent
Android Fragments:
Android Fragment is the part of activity, it is also known as sub-activity. There
can be more than one fragment in an activity. Fragments represent multiple
screen inside one activity.
Android fragment lifecycle is affected by activity lifecycle because fragments
are included in activity.
Each fragment has its own life cycle methods that is affected by activity life
cycle because fragments are embedded in activity.
11
Android Menu :
13
1. Option Menu
Android Option Menus are the primary menus of android. They can be used
for settings, search, delete item etc.
Here, we are going to see two examples of option menus. First, the simple
option menus and second, options menus with images.
Here, we are inflating the menu by calling the inflate() method
of MenuInflater class. To perform event handling on menu items, you need to
override onOptionsItemSelected() method of Activity class.
2. Context Menu
Android context menu appears when user press long click on the element. It is
also known as floating menu.
It affects the selected content while doing action on it.
It doesn't support item shortcuts and icons.
3. Popup Menu
Android Popup Menu displays the menu below the anchor text if space is
available otherwise above the anchor text. It disappears if you click outside the
popup menu.
The android.widget.PopupMenu is the direct subclass of java.lang.Object class.
Android Service :
2. Bound
1) Started Service
A service is started when component (like activity) calls startService() method,
now it runs in the background indefinitely. It is stopped
by stopService() method. The service can stop itself by calling
the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client)
calls bindService() method. The client can unbind the service by calling
the unbindService() method.
The service cannot be stopped until all clients unbind the service.
Android SQLite :
SQLite is an open-source relational database i.e. used to perform database
operations on android devices such as storing, manipulating or retrieving
persistent data from the database.
It is embedded in android bydefault. So, there is no need to perform any
database setup or administration task.
Here, we are going to see the example of sqlite to store and fetch the data.
Data is displayed in the logcat. For displaying data on the spinner or listview,
move to the next page.
SQLiteOpenHelper class provides the functionality to use the SQLite
database.
SQLiteOpenHelper class:
The android.database.sqlite.SQLiteOpenHelper class is used for database
creation and version management. For performing any database operation,
you have to provide the implementation
of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
Constructors of SQLiteOpenHelper class
There are two constructors of SQLiteOpenHelper class.
Constructor Description
Method Description
json object:
A JSON object contains key/value pairs like map. The keys are strings and the
values are the JSON types. Keys and values are separated by comma. The
{ (curly brace) represents the json object.
1. {
2. "employee": {
3. "name": "sachin",
4. "salary": 56000,
5. "married": true
6. }
7. }
json array:
The [ (square bracket) represents the json array.
1. ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday
"]
Let's take another example of json array.
1. { "Employee" :
2. [
17
3. {"id":"101","name":"Sonoo Jaiswal","salary":"50000"},
4. {"id":"102","name":"Vimal Jaiswal","salary":"60000"}
5. ]
6. }
Android Speech:
A. Text to Speech
In android, you can convert your text into speech by the help
of TextToSpeech class. After completion of the conversion, you can playback
or create the sound file.
Constructor of TextToSpeech class:
TextToSpeech(Context context, TextToSpeech.OnInitListener)
Methods of TextToSpeech class:
The commonly used methods of TextToSpeech class are as follows:
Method Description
int speak (String text, int converts the text into speech. Queue Mode may be
queueMode, HashMap params) QUEUE_ADD or QUEUE_FLUSH. Request parameters
can be null, KEY_PARAM_STREAM,
KEY_PARAM_VALUME etc.
int setLanguage (Locale loc) it sets the locale specific language for the speech.
TextToSpeech.OnInitListener Interface
You need to implement TextToSpeech.OnInitListener interface, for performing
event handling on TextToSpeech engine.
Method of TextToSpeech.OnInitListener Interface
There is only one method in this interface.6Montinue Explosive Growth
Method Description
void onInit (int Called to signal the completion of the TextToSpeech engine
status) initialization. The status can be SUCCESS or ERROR.
B. MediaPlayer class
The android.media.MediaPlayer class is used to control the audio or video
files.
Methods of MediaPlayer class:
There are many methods of MediaPlayer class. Some of them are as
follows:Difference between JDK, JRE, and JVM
Method Description
public void setDataSource(String path) sets the data source (file path or
http url) to use.
public void setLooping(boolean looping) sets the player for looping or non-
looping.
19
C. Video Player
By the help of MediaController and VideoView classes, we can play the video
files in android.
MediaController class:
The android.widget.MediaController is a view that contains media controls
like play/pause, previous, next, fast-forward, rewind etc.
VideoView class:
The android.widget.VideoView class provides methods to play and control
the video player. The commonly used methods of VideoView class are as
follows:
Method Description
public void setVideoURI (Uri uri) sets the URI of the video file.
D. MediaRecorder class:
MediaRecorder class can be used to record audio and video files.
After recording the media, we can create a sound file that can be played later.
In this example, we are going to record the audio file and storing it in the
external directory in 3gp format.
Android Telephony:
Android TelephonyManager Class
The android.telephony.TelephonyManager class provides information about
the telephony services such as subscriber id, sim serial number, phone network
type etc. Moreover, you can determine the phone state etc.
Android Call State
We can also get the information of call state using
the TelephonyManager class. For this purpose, we need to call the listen
method of TelephonyManager class by passing the PhonStateListener
instance.
The PhoneStateListener interface must be implemented to get the call state.
It provides one method onCallStateChanged().
2. Intent intent=new Intent(getApplicationContext(),MainActivity.class);
3. PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,
0);
4. //Get the SmsManager instance and call the sendTextMessage method to sen
d message
5. SmsManager sms=SmsManager.getDefault();
6. sms.sendTextMessage("8802177690", null, "hello javatpoint", pi,null);
Methods Description
callback) map.
5. Mediator Pattern
6. Memento Pattern
7. Observer Pattern
8. State Pattern
9. Strategy Pattern
10.Template Pattern
11.Visitor Pattern