0% found this document useful (0 votes)
10 views

Module 5 Concpets

Uploaded by

MANJUNATHA H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 5 Concpets

Uploaded by

MANJUNATHA H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

IN DETAIL “SERVICES” CONCEPT -Module 5

What are services?

− A service is a component which runs in the background without direct interaction with the user.
As the service has no user interface, it is not bound to the lifecycle of an activity.
− Services are used for repetitive and potentially long running operations, i.e., Internet
downloads, checking for new data, data processing, updating content providers and the like.

• Types of Services

Two ways of moving task to background


(i) Using Handler class
(ii) Using AsyncTask class

(i) Using Handler class


We mainly use the Handlers to
− Update the User Interface from a background thread,
− Enqueue a task for a different thread
− Schedule a task for a specified time.

(ii) Using AsyncTask class

In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background
and then synchronize again with our main thread. This class will override at least one method i.e
doInBackground(Params) and most often will override second method onPostExecute(Result).

Method of AsyncTask In Android:

1. doInBackground(Params) – This method is invoked on the background thread immediately


after onPreExecute() finishes its execution. Main purpose of this method is to perform the
background operations that can take a long time.

2. onProgressUpdate(Progress…) – This method is invoked on the main UI thread after a call


to publishProgress(Progress…). Timing of the execution is undefined. This method is used to
display any form of progress in the user interface while the background operations are executing.

3. onPostExecute(Result) – This method is invoked on the main UI thread after the background
operation finishes in the doInBackground method.

4. onPreExecute() – It invoked on the main UI thread before the task is executed. This method is
mainly used to setup the task for instance by showing a ProgressBar or ProgressDialog in the
UI(user interface).

2. Content Providers

What Is a Content Provider


− A content provider acts as a data store and provides an interface to access its contents.
− It encapsulates data and provide it to applications through the single Content Resolver
interface.
− A content provider is only required if you need to share data between multiple
applications.
− Example: the contacts data is used by multiple applications and must be stored in a
content provider. If you don't need to share data amongst multiple applications you can use a
database directly via SQLiteDatabase.

• Characteristics of Content Provider


− Like the database we can query, add, edit, delete and update data in content provider.
− Content providers are not limited to texts, but also contains images and videos as well.
− Data can be stored in a data base, files and over a network.
− Content providers act as wrapper, that is data in Content providers is exposed as
service.

Using Content Providers: Accessing contact information.

How to insert contact information. (VVIMP)


 Contact are named as people in the emulator
 To access contact information, click people icon on home screen as
shown in figure 5.2
 A page is displayed as showing “no contacts exist”
 Three buttons are displayed on the screen: “create a new contact”,
“sign into an account”, and “import contacts”
Select create new contact and enter relevant information on the
dialog box that appears.
Once all contacts added, The contacts information accessed from
the device/emulator is displayed vis ListView as shown in figure 5.3

Steps for Creating a custom Content Providers:


Steps taken to create our own Content provider. Define the content
provider.
Define the database, URIs, column names, MIME types.
Implement the query (insert/update/delete)
Register the provider in a manifest file.
Step1: Define the content provider.

To define content provider, lets create new Android project.


− On startup to display the user interface create the xml lay out file as

Step2: Defining database and URI.


Step3: Implement the Query

5.1.3. UNDERSTANDING BROADCAST RECEIVERS


A broadcast receiver is a component that responds to different messages
that are broadcast by the system or other applications. Messages such as
new mail, low battery, captured photo, or completed download require
attention.
We cover two separate aspects of broadcast receivers:
Broadcasting an Intent
Receiving the broadcast Intent

a) Broadcasting an Intent
To broadcast an Intent, we first create an Intent object, assign a specific
action to it, attach data or a message to the broadcast receiver, and finally
broadcast it. We can optionally put an extra message or data on the Intent.
Table 11.1 lists the methods involved in broadcasting an Intent.
b) Receiving the Broadcast Intent
A broadcast receiver is a class that extends the BroadcastReceiver. It also
needs to be registered as a receiver in an Android application via the
AndroidManifest.xml file or through code at runtime. The broadcast receiver
class needs to implement the onReceive() method
The getAction() and getStringExtra() methods used here need some
explanation:
o getAction() - Retrieves the action to be performed from the Intent object.
It is the action that indicates what task has to be performed on the data
passed along with the Intent.

Syntax: getAction()

o getStringExtra() - Retrieves the extended data from the Intent.

Syntax: getStringExtra(String name)

where name represents the key or the name assigned to the value
while adding data to the Intentthrough the putExtra() method.

You might also like