Android Intentservice vs Service

Source: Internet
Author: User

Android Intentservice vs Service

As is known to all, the service in Android is used for background services, when the application is suspended in the background, in order to ensure that some features in the app can still work and introduced the service, such as playing music. For service, official documentation has 2 important notes:
1. A Service is a separate process. The Service object itself does not imply it was running in its own process; Unless otherwise specified, it runs in the same process as the application it was part of. Unless otherwise noted, the application is within the same process
2. A Service is not a thread. It is a means itself to does work off of the main thread (to avoid application not responding errors). The service is not a thread, which means that the service and UI main thread are running in the same threads, so the service cannot perform lengthy processing to prevent ANR.
So if the app needs to take a long time to process the service, it needs a separate thread to handle the operation.
Android has taken this into account for us, so we have provided the Intentservice class. Intentservice inherits from the service and can be thought of as opening a thread to handle the service of the event.
Intentservice has the following advantages:
1. Intentservice is a base class for Services, handle asynchronous requests (expressed as Intents) on demand. Clients send requests through StartService (Intent) calls; The service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of Work. The queue task executes sequentially and ends when all tasks are completed.
2. This "Work Queue processor" pattern was commonly used to offload the tasks from a application ' s main thread. The Intentservice class exists to simplify this pattern and take care of the mechanics. To use it, extend Intentservice and implement Onhandleintent (Intent). Intentservice would receive the Intents, launch a worker thread, and stop the service as appropriate.
3. All requests is handled on a single worker Thread–they could take as long as necessary (and would not block the Applica tion ' s main loop), but only one request would be processed at a time. All requests are completed in one worker thread, but once after a single one is processed.
Below we analyze the Intentservice code:
Inherit from service
Public abstract class Intentservice extends Service
There is a worker thread that creates the thread in OnCreate
Handlerthread thread = new Handlerthread ("intentservice[" + Mname + "]");
Internally, the message events sent by the StartService are handled by handler. Also create the thread looper and handler in the OnCreate function
Mservicelooper = Thread.getlooper ();
Mservicehandler = new Servicehandler (mservicelooper);

The task is added to the queue each time StartService is called

publicvoidonStartint startId) {        Message msg = mServiceHandler.obtainMessage();        msg.arg1 = startId;        msg.obj = intent;        mServiceHandler.sendMessage(msg);}

The handler execution process is:

privatefinalclass ServiceHandler extends Handler {        publicServiceHandler(Looper looper) {            super(looper);        }        @Override        publicvoidhandleMessage(Message msg) {            onHandleIntent((Intent)msg.obj);            stopSelf(msg.arg1);        }}

From the above, the Intentservice is executed in order, and the worker thread finishes after all the tasks have been executed.

Use the following:
Add a claim in Manifext.xml

Then use it directly in the place where it was called:

publicvoidstartService() {  new Intent(this, XXXIntentService.class);  publicvoidstopService() {  new Intent(this, XXXIntentService.class);  stopService(intent); }

Where call StopService calls Intentservice's OnDestroy function directly, the currently executing task is not paused, and the thread shuts down when the task completes. If you need to end the task immediately, you can set the variable to let the current task end as soon as possible.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Intentservice vs 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.