Module 5 Concpets
Module 5 Concpets
− 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
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).
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
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()
where name represents the key or the name assigned to the value
while adding data to the Intentthrough the putExtra() method.