Android Official Development Document Training Series Course Chinese version: Intentservice creation of background service

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/run-background-service/index.html

Introduction

Unless specifically specified, all operations are performed in the UI thread. This can cause problems, however, because lengthy time-consuming operations can prevent the UI thread from running. This will annoy the user and may cause system errors. To avoid this, Android provides classes that allow these time-consuming operations to be performed in a separate thread. The most used here is the intentservice.

This lesson focuses on how to implement Intentservice, how to send a work request to it, and how to respond to its execution results.

Create a background service

Intentservice provides a very simple construction method. Intentservice allows time-consuming operations to be performed without causing the UI thread to block. Similarly, Intentservice is not affected by the UI life cycle. So it can run continuously in a single environment.

However, the Intentservice is also limited, listed as follows:

    • It can not interact directly with the UI thread. To deliver the results to the UI, you have to send the results in a broadcast format.
    • Work requests are executed sequentially. If there is already an operation in Intentservice, then the operation request will wait until the first operation is completed.
    • The operation in the Intentservice can not be interrupted.

However, in most cases, intentservice is the preferred way to perform background operations.

This lesson focuses on how to implement Intentservice, how to create a request callback method Onhandleintent (), and finally we will learn how to declare the Intentservice in the manifest file.

Create Intentservice

You first need to create a class and inherit Intentservice, and then rewrite its onhandleintent () method:

publicclass RSSPullService extends IntentService {@OverrideprotectedvoidonHandleIntent// Gets data from the incoming Intent// Do work here, based on the contents of dataString        ...    }}

It is important to note that the callback methods of the ordinary service, such as the Onstartcommand () method, are automatically called by Intentservice, so it is best not to rewrite these methods inside the Intentservice.

Declaring Intentservice in the manifest file

The intentservice also needs to be defined in the manifest file. It is defined in the same way as the normal service.

<application android:icon= "@drawable/icon" android:label="@st Ring/app_name ">...<!--Because android:exported is set to "false", and the service is only available to this app. - <service android:name=". Rsspullservice " android:exported=" false "/>...<application/>

Where the Android:name property describes the class name of Intentservice.

Note here: The service tag does not contain the intent Filter. Because other components send work requests by displaying intent. So there is no need to define intent Filter. This also means that only apps within the relevant component or programs with the same user ID can access the service.

Now that we have defined a Intentservice class, we can then send a work request to it through the intent object. Information about how to build related objects and how to send requests to Intentservice will be learned in the next lesson.

Android Official Development Document Training Series Course Chinese version: Intentservice creation 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.