Android IntentService vs Service, intentservice

Source: Internet
Author: User

Android IntentService vs Service, intentservice
Android IntentService vs Service

As we all know, services in Android are used for background services. When an application is mounted to the background, services, such as playing music, are introduced to ensure that some functions in the application can still work. For the service, the official documentation has two important notes:
1. A Service is not a separate process. the Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part. unless otherwise specified, it is in the same process as the application.
2. A Service is not a thread. it is not a means itself to do work off of the main thread (to avoid Application Not Responding errors ). the Service is not a thread, that is, the service and the main UI thread are running in the same thread. Therefore, the service cannot execute long-time processing to prevent ANR from occurring.
Therefore, if the APP needs to process the service for a long time, it needs an independent thread to process the operation.
Android provides the IntentService class. IntentService inherits from Service and can be considered as a service with a thread to process events.
IntentService has the following advantages:
1. intentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. clients send requests through startService (Intent) CALS; 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 tasks are executed in order. After all tasks are completed, they are terminated.
2. this "work queue processor" pattern is commonly used to offload tasks from an application's main thread. the IntentService class exists to simplify this pattern and take care of the mechanic. to use it, extend IntentService and implement onHandleIntent (Intent ). intentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
3. all requests are handled on a single worker thread-they may take as long as necessary (and will not block the application's main loop ), but only one request will be processed at a time. all requests are completed in one work thread, but are processed once.
Next we will analyze the IntentService code:
Inherited from service
Public abstract class IntentService extends Service
There is a working thread that is created in onCreate
HandlerThread thread = new HandlerThread ("IntentService [" + mName + "]");
Internally, handler is used to process the message events sent by startService. Create thread logoff and Handler in the onCreate function.
Mserviceloaders = thread. getLooper ();
MServiceHandler = new ServiceHandler (mserviceloader );

The task is added to the queue each time startService is called.

public void onStart(Intent intent, int startId) {        Message msg = mServiceHandler.obtainMessage();        msg.arg1 = startId;        msg.obj = intent;        mServiceHandler.sendMessage(msg);}

Handler execution process:

private final class ServiceHandler extends Handler {        public ServiceHandler(Looper looper) {            super(looper);        }        @Override        public void handleMessage(Message msg) {            onHandleIntent((Intent)msg.obj);            stopSelf(msg.arg1);        }}

It can be seen from the above that IntentService is executed in order, and the worker thread finishes after all tasks are executed.

Use:
Add declaration in manifext. xml

Then, use the following directly at the place of the call:

public void startService() {  Intent intent = new Intent(this, XXXIntentService.class);  startService(intent); } public void stopService() {  Intent intent = new Intent(this, XXXIntentService.class);  stopService(intent); }

When stopService is called, The onDestroy function of IntentService is called directly, and the currently running task is not paused. The thread is closed after the task is completed. If you need to end the task immediately, you can set variables to end the current task as soon as possible.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.