IntentService practical application scenarios

    IntentService compared to the parent Service, the biggest feature is its callback function onHandleIntent can be time-consuming operation directly, without having to re-open thread. The principle is IntentService member variable is initialized when Handler already belongs to the worker thread, after handleMessage, including onHandleIntent other functions run in the worker thread.

    If you know of IntentService of limited, will be a kind IntentService very sad point of view, because the time-consuming operation Service in the open threads no trouble. I also had this point of view, it is rarely used IntentService.

    But IntentService there is a feature that is called multiple times onHandleIntent function (that is, there are more time-consuming tasks to be performed), more time-consuming tasks are performed in the order. The principle is associated with its built-in task queue Handler, Handler looper performed by taking tasks are executed sequentially.

    This feature will solve the problem more time-consuming tasks required in the order of execution. If only the service, open multiple threads to perform time-consuming operation, it is difficult to manage.

    For example, in doing before the app, there is a demand for a certain period of time is saved download user picture displayed imageView after the download is complete; the general need to download the picture there are many, each of the next picture is a thread, immediately after downloading display out, so definitely want to download is downloaded according to the order, so the user experience is better. The approach was defective, directly open multiple threads to download, the download is complete before a picture in front, other threads must wait; although this function can also be achieved, but not high efficiency, even ANR (the specific code logic is possible If the thread gets behind the cpu, but not in front of the picture is finished, wait; not before assuming that a picture is finished, while the back of the thread has been obtained cpu, there is a problem).

    If this scenario is very easy to use IntentService, each consuming tasks are performed sequentially in the order, do not have to worry about logic or performance occurs, it is IntentService of value.


Summary: Android Many classes are designed for certain common scenarios, such as in this article IntentService and handlerThread and other written before the class, usually accumulate in order to be solved later when encountered some difficult issues.

Published 11 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/zhengyin_tmac/article/details/52446696