Android uses Intentservice to create a background service instance _android

Source: Internet
Author: User

Intentservice provides a simple structure for running operations on a single background thread. This allows it to operate time-consuming operations without affecting the UI response. Similarly, Intentservice does not affect the UI lifecycle event, so it will continue to run in some cases where the asynctask is likely to be closed (measured in the ondestory of the activity Asynctask cannot run).

Intentservice has the following limitations:

1. It does not directly affect the UI. To reflect the results to the UI, you need to send the activity
2. Work requests will be run sequentially. If an operation does not end, the operation that is sent later must wait for it to end (single thread)
Operation in 3.IntentService cannot be interrupted

However, in most cases, intentservice is the preferred way for simple background tasks.

This section shows how to create Intentservice subclasses, how to create onhandleintent () callbacks, and how to declare intentservice in Androidmanifest.xml.

Create Intentservice

Defines a intentservice subclass that overrides the Onhandleintent () method:

Copy Code code as follows:

public class Rsspullservice extends Intentservice {
@Override
protected void Onhandleintent (Intent workintent) {
Gets data from the incoming Intent
String datastring = workintent.getdatastring ();
...
Do work this, based on the contents of datastring
...
}
}

Hint: The normal callback of other service, like Onstartcommand () will be invoked automatically in Intentservice. In Intentservice, you should avoid overwriting these callbacks.

Define Intentservice in Androidmanifest.xml.

Intentservice is also a service) and needs to be registered in Androidmanifest.xml.

Copy Code code as follows:

<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/>

The Android:name property specifies the class name of the Intentservice.

Note: The &LTSERVICE&GT node cannot contain intent filter. The activity that sends the work request uses a clear intent, which specifies which Intentservice. This also means that only the components in the same app, or another application with the same user ID, can access intentservice.

Now that you have the base Intentservice class, you can send a work request with a intent object.

Create a Send work request to Intentservice

Create a clear intent, add the required data, and invoke StartService () to send to Intentservice

Copy Code code as follows:
/*
* Creates a new Intent to start the Rsspullservice
* Intentservice. Passes a URI in the
* Intent ' s "Data" field.
*/
Mserviceintent = new Intent (getactivity (), rsspullservice.class);
Mserviceintent.setdata (Uri.parse (Dataurl));
Call StartService ()
Starts the Intentservice
Getactivity (). StartService (mserviceintent);

Tip: You can send a work request anywhere in the activity or fragment. If you need to get user input first, you can send a job request in a click event or a callback method like gesture.

Once the StartService () is invoked, Intentservice will work in onhandleintent () and end itself.

The next step is to report the results to the original activity or fragment, the next section on how to use Broadcastreceiver implementation. Please refer to this article: http://www.jb51.net/article/51548.htm

Related Article

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.