Android Services 2
Dr. Miguel Angel Len Chvez
Android
BUAP - FCC
MALCH
Creating a Background Service
The
IntentService class provides a straightforward
structure for running an operation on a single
background thread.
This allows it to handle long-running operations
without affecting your user interface's
responsiveness.
Also, an IntentService isn't affected by most user
interface lifecycle events, so it continues to run in
circumstances that would shut down an AsyncTask
Android
BUAP - FCC
MALCH
Creating a Background Service
An
IntentService has a few limitations:
It can't interact directly with your user interface. To put its
results in the UI, you have to send them to an Activity.
Work requests run sequentially. If an operation is running
in an IntentService, and you send it another request, the
request waits until the first operation is finished.
An operation running on an IntentService can't be
interrupted.
However,
in most cases an IntentService is the
preferred way to simple background operations.
Android
BUAP - FCC
MALCH
Create an IntentService
public class RSSPullService extends IntentService {
"
"@Override "
"protected void onHandleIntent(Intent workIntent) {
"
"
"// Gets data from the incoming Intent
"String dataString=[Link]();
"
"
"... "
"
"// Do work here, based on the contents of
dataString "
"
"... "
"
"}"
}
Android
BUAP - FCC
MALCH
Define the IntentService in the Manifest
<application "
"android:icon="@drawable/icon" "
"android:label="@string/app_name"> "
"... "
"<!-- "
"
"Because android:exported is set to "false,
the service is only available to this app.
"
"--> "
"<service "
"
"android:name=".RSSPullService" "
"
"android:exported="false"/> " "
"
"... "
<application/>
Android
BUAP - FCC
MALCH
Sending Work Requests to the
Background Service
To
create a work request and send it to an
IntentService, create an explicit Intent, add work
request data to it, and send it to IntentService by
calling startService().
Android
BUAP - FCC
MALCH
Create and Send a Work Request to an
IntentService
1.
Create a new, explicit Intent for the IntentService
called RSSPullService.
/*Creates a new Intent to start the"
* RSSPullServiceIntentService. Passes a"
* URI in theIntent's "data" field."
*/"
mServiceIntent = new Intent(getActivity(),
[Link]);"
[Link]([Link](dataUrl));
Android
BUAP - FCC
MALCH
Create and Send a Work Request to an
IntentService
2.
Call startService()
// Starts the IntentService"
getActivity().startService(mServiceIntent);"
"
Notice that you can send the work request from anywhere in
an Activity or Fragment.
Once you call startService(), the IntentService does the work
defined in its onHandleIntent() method, and then stops itself.
Android
BUAP - FCC
MALCH
Reporting Work Status
To
send the status of a work request in an
IntentService to other components, first create an
Intent that contains the status in its extended data. As
an option, you can add an action and data URI to this
Intent.
Next, send the Intent by calling
[Link]().
Android
This sends the Intent to any component in your application
that has registered to receive it. To get an instance of
LocalBroadcastManager, call getInstance().
BUAP - FCC
MALCH
Reporting Work Status
Android
BUAP - FCC
MALCH
10
Receive Status Broadcasts from an
IntentService
To
receive broadcast Intent objects, use a subclass
of BroadcastReceiver. In the subclass, implement
the [Link]() callback
method, which LocalBroadcastManager invokes
when it receives an Intent.
LocalBroadcastManager passes the incoming
Intent to [Link]().
Android
BUAP - FCC
MALCH
11
Receive Status Broadcasts from an
IntentService
// Broadcast receiver for receiving status updates from the
IntentService"
private class ResponseReceiver extends BroadcastReceiver"
{ "
"// Prevents instantiation "
"private DownloadStateReceiver() { "
"} "
// Called when the BroadcastReceiver gets an Intent it's
registered to receive "
"@public void onReceive(Context context, Intent intent) {"
"
"
"... "
"
"
"/* "
"
"
"* Handle Intents here. "
"
"
"*/"
"
"
"... "
"
"}"
}
Android
BUAP - FCC
MALCH
12
Receive Status Broadcasts from an
IntentService
Android
Once you've defined the BroadcastReceiver, you can
define filters for it that match specific actions, categories,
and data. To do this, create an IntentFilter.
BUAP - FCC
MALCH
13
Receive Status Broadcasts from an
IntentService
Android
To register the BroadcastReceiver and the IntentFilter with
the system, get an instance of LocalBroadcastManager and
call its registerReceiver() method.
BUAP - FCC
MALCH
14
Receive Status Broadcasts from an
IntentService
A
single BroadcastReceiver can handle more than
one type of broadcast Intent object, each with its
own action.
This feature allows you to run different code for each
action, without having to define a separate
BroadcastReceiver for each action.
To
define another IntentFilter for the same
BroadcastReceiver, create the IntentFilter and
repeat the call to registerReceiver().
Android
BUAP - FCC
MALCH
15
Receive Status Broadcasts from an
IntentService
Android
BUAP - FCC
MALCH
16
Receive Status Broadcasts from an
IntentService
Sending an broadcast Intent doesn't start or resume an
Activity.
The BroadcastReceiver for an Activity receives and
processes Intent objects even when your app is in the
background, but doesn't force your app to the
foreground.
If you want to notify the user about an event that
happened in the background while your app was not
visible, use a Notification.
Never start an Activity in response to an incoming
broadcast Intent.
Android
BUAP - FCC
MALCH
17
References
Android
[Link]
[Link]
BUAP - FCC
MALCH
18