0% found this document useful (0 votes)
42 views25 pages

Chapter Five

It is very useful

Uploaded by

amentiabraham674
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)
42 views25 pages

Chapter Five

It is very useful

Uploaded by

amentiabraham674
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/ 25

Chapter 5

Android Communications Via Network and


the Web

By Chalew Z.(MSc.) 1
Android Communication,
network and web
Outline
Android Telephony

Android Notifications and


Alarms

By Chalew Z.(MSc.)
Communication, network and web
 Communication with Internet access was one of the biggest
motivators behind the Smartphone takeover
 Your application may want to access the internet in a couple ways:
 Hyperref to an existing webpage
 View an existing webpage in the application
 Download data to populate views in your app
 Backup data from your application to the Internet
 Pattern 1: Hyperref to an existing webpage:
 Create Intent:
 Set Action to ACTION_VIEW
 Add Category CATEGORY_BROWSABLE
 Set Data to URL by parsing the URL into a URI
By Chalew Z.(MSc.) 3
Cont..
Pattern 2: Using A WebView
 WebView – A view that displays web pages
 Can roll your own web browser or display online content
 Must request the Internet permission in manifest

“android.permission.INTERNET”
“android.permission.ACCESS_NETWORK_STATE”

By Chalew Z.(MSc.) 4
Cont..
 Pattern 3: Downloading the Internet
 The WebView and WebBroswer Intent have HTTP Client
functionality built in
 However, they are’t as light weight as your application may need
 So, you need the following steps to perform network operations:
 Design secure network communication
 Choose HTTP Client
 Perform network operations on a separate thread
 Fetch data
 Network access uses limited resources: Battery, Data, and Time
 You should develop your network access protocols to minimize the effect on
these resources

By Chalew Z.(MSc.) 5
Android Network Connectivity Services

Android network connectivity services allow us to check the


network connectivity information of the device.
It is very important to check the internet connection of the device
while performing the task which is based on internet service such as
fetching data from the server (internet) or writing data to the
server.
Using Android Network Connectivity Services we can also
determine the types of a network of android device. It may be of
types
TYPE_WIFI (wifi),
TYPE_MOBILE (mobile),
TYPE_BLUETOOTH (Bluetooth), etc.

By Chalew Z.(MSc.) 6
Cont..
Android Network Connectivity Service Example
Let's create a simple example to check the network connectivity of
the device as well as its type.

Note that to access the network connectivity of a device, your


application AndroidMenifest.xml file must include the following
permissions:

By Chalew Z.(MSc.) 7
Checking Network Connection
Android Network Connectivity Service Example
In android, by using the ConnectivityManager class we can easily
determine whether the device connected to the network/internet or
not and

Also we can determine the type of internet connection currently


available i.e. whether it’s mobile data or Wi-Fi.

You need to instantiate an object of this class by calling


getSystemService() method this connected states, there are other
states a network can achieve.

They are Connecting, Disconnected, Disconnecting, Suspended


and Unknown
By Chalew Z.(MSc.) 8
Cont..

Its syntax is given below:

Once you instantiate the object of ConnectivityManager class, you


can use getAllNetworkInfo method to get the information of all the
networks.

This method returns an array of NetworkInfo. So you have to


receive it like this.

By Chalew Z.(MSc.) 9
Cont..

The last thing you need to do is to check Connected State of the


network.

Its syntax is given below:


for (int i = 0; i<info.length; i++){
if (info[i].getState() == NetworkInfo.State.CONNECTED){
Toast.makeText(context, "Internet is connected Toast.LENGTH_SHORT).show();
}
}

By Chalew Z.(MSc.) 10
Performing Network Operations
After checking that you are connected to the internet, you can
perform any network operation.

Here we are fetching the html of a website from a url.


Android provides HttpURLConnection and URL class to handle these
operations.

You need to instantiate an object of URL class by providing the link of


website this connect method, there are other methods available in
HttpURLConnection class.

Its syntax is as follows:

By Chalew Z.(MSc.) 11
Cont..
After that you need to call openConnection method of url class and
receive it in a HttpURLConnection object.
After that you need to call the connect method of HttpURLConnection
class.

And the last thing you need to do is to fetch the HTML from the website.
For this you will use InputStream and BufferedReader class. Its syntax is
given below:

InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String page = "",data="";
while ((data = reader.readLine()) != null){
page += data + "\n";
}

By Chalew Z.(MSc.) 12
By Chalew Z.(MSc.) 13
Cont..

The methods available in HttpURLConnection class are:

By Chalew Z.(MSc.) 14
Android Telephony
 Android Telephony framework provides us the functionalities of the
mobile.

• It gives us information about functionalities like calls,


SMS, MMS, network, data services, IMEI number, and
so on.
• For better understanding, you can consider Dialer,
Browser, Sim App toolkit, Broadcast receivers, and so on.
• Architecture of Android Telephony
• Android Telephony Manager is one such class that is
present in the android.telephony package.

By Chalew Z.(MSc.) 15
Architecture of Android Telephony
 The architecture of Android Telephony is subdivided into four
layers.
 These four layers help it interact with users and low-level devices like
your network adapters.

By Chalew Z.(MSc.) 16
Cont..
Communication Processor
It is an input/output processor to distribute and collect data from a
number of remote terminals.
It is a specialized processor designed to communicate with the data
communication network.
Radio Interface Layer(RIL)
It is a bridge between the hardware and Android phone framework
services. Rather we say, it is a protocol stack for Telephone.
It has two main components that are:
1. RIL Daemon: It starts when the android system starts. It reads
the system properties to find a library that is to be used for Vendor
RIL.
2. Vendor RIL: It is also known as RIL Driver. It can be understood
as a library that is specific to each modem.

By Chalew Z.(MSc.) 17
Cont..
Framework Services
 Framework services consist of various packages and assists the
Telephony Manager in directing all the application API requests to
RIL. The framework services start immediately when the device
boots up.
Application
It is the last layer of the Telephony architecture. The applications
are used directly by the user to interact with the Telephony
Services. Some of the standard applications are IMEI checker,
Dialer, SIM Toolkit, Browser, etc.

By Chalew Z.(MSc.) 18
Implementation of Android Telephony

Now, let’s see how you can implement the telephony and get
information about your device and subscribers. Follow the below
steps:
1. Run your Android Studio and create a new project.
2. Select Empty Activity, name your project and choose API level and
proceed.
3. After the Gradle build is finished, open your AndroidManifest.xml
file and add the permissions below.

By Chalew Z.(MSc.) 19
Notifications
Notification a is a message which is displayed to the user outside
of the normal User Interface

By Chalew Z.(MSc.) 20
Cont…
Notification Manager
Android allows putting notification into the title bar of your
application.
The user can expand the notification bar and by selecting the
notification the user can trigger another activity.
Setting up Notifications
Notifications in Android are represented by the Notification class.
To create notifications you use the NotificationManager class
which can be received from the Context, e.g. an activity or a service,
via the getSystemService() method.

By Chalew Z.(MSc.) 21
Cont..
The Notification.Builder provides a builder interface to create an
Notification object.
Created using the Builder.build() function, which returns a
Notification object with the specifications Issued using the
NotificationManager.notify() method
A notification must contain the following:
• setSmallIcon() – sets the small icon
• setContentTitle() – Title for the notification
• setContentText() – sets the detail text
• setChannelId() – set a channel (for API 26 and above)

By Chalew Z.(MSc.) 22
Cont..
You use a PendingIntent to specify the action which should be
performed once the user selects the notification.
Set the icon, scrolling text and timestamp

Pending Intent
A PendingIntent is a token that you give to another application
(e.g. Notification Manager, Alarm Manager or other 3rd party
applications), which allows this other application to use the
permissions of your application to execute a predefined piece of
code.

By Chalew Z.(MSc.) 23
Alarm In Android

 An alarm is used to wake the Android System and perform


some kind of operation
 Operation is handled through the Alarm Manager

 Passive and Active Behavior


 Passive transactions are initiated by the user while
 Active transactions are initiated by the system

By Chalew Z.(MSc.) 24
Cont..
 Active transactions can be defined by these properties:

 Transaction initiated by system, user is given an


opportunity to respond
 Require timely response from user
 Interactions are sequential and serial
 Between system and a single user

By Chalew Z.(MSc.) 25

You might also like