Original address: http://android.xsoftlab.net/training/run-background-service/send-request.html
Last lesson we learned how to create a intentservice. In this lesson we mainly learn how to make Intentservice perform work requests through intent. Intent can hand over any data to intentservice processing. You can send intent to intentservice in any method of activity or fragment.
Create and send a work request to Intentservice
To create a work request and send it to intentservice, we first need to create a displayed intent object, add the request data to it, and then send it to Intentservice by StartService ().
The following code demonstrates this process:
-
- Create a displayed intent for the intentservice named Rsspullservice.
/* * Creates a new Intent to start the RSSPullService * IntentService. Passes a URI in the * Intent‘s "data" field. */new Intent(getActivity(), RSSPullService.class);mServiceIntent.setData(Uri.parse(dataUrl));
// Starts the IntentServicegetActivity().startService(mServiceIntent);
Note that you can send work requests anywhere in the activity or fragment. For example, if you need to get the user's input data first, you can place the sending code of the work request in the click callback of the button.
Once StartService () is called, Intentservice will execute the work request within the Onhandleintent () method, and it will stop automatically after the task completes.
The next step is to feed the completion results of the work back to the request call. The next lesson will learn how to use Broadcastreceiver to complete the process.
Android Official Development Document Training Series Course Chinese version: use of intentservice of background service