Android Official Development Document Training Series Course Chinese version: use of intentservice of background service

Source: Internet
Author: User

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:

      1. 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));
      1. Call StartService ().
// 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.