IntentService for Android asynchronous loading full resolution

Source: Internet
Author: User

IntentService for Android asynchronous loading full resolution
Android asynchronous loading of full parsing IntentService what kind of IntentService we have mentioned before. asynchronous processing uses the specified AsyncTask and Thread, So what a ghost is this IntentService. Compared with the two asynchronous loading methods mentioned above, IntentService has the biggest feature: IntentService is not affected by most UI lifecycles, it provides a more direct operation method for background threads. However, the shortcomings of IntentService are mainly reflected in the following points:
You cannot directly interact with the UI. To reflect the execution result on the UI, you need to send it to the Activity. The job queue is executed sequentially. if a job is being executed in IntentService, you can send another job request until the previous job is completed. An ongoing task cannot be interrupted. Create IntentService when we create a class and inherit the IntentService, we can generate the following code through the IDE prompt:

package com.imooc.intentservicetest;import android.app.IntentService;import android.content.Intent;public class MyIntentService extends IntentService {    /**     * Creates an IntentService.  Invoked by your subclass's constructor.     *     * @param name Used to name the worker thread, important only for debugging.     */    public MyIntentService(String name) {        super(name);    }    public MyIntentService() {        super(MyIntentService);    }    @Override    protected void onHandleIntent(Intent intent) {    }}

PS: note that the system will prompt us to generate a constructor with parameters. In addition, we also need to define a constructor without parameters and call a constructor with parameters. Therefore, a constructor with parameters can be deleted.
The most important thing is:
@Overrideprotected void onHandleIntent(Intent intent) {}

In this method, we obtain data from intent and perform corresponding operations.
Ps: IntentService inherits from the Service, but we do not need to manually call Service callback methods such as onStartCommand.
Declare IntentService we need to declare the IntentService in the Mainifest file:
 

Similar to declaring a Service, but does not need to declare Because explicit Intent is required for the Activity that sends tasks to IntentService, no filter is required. This means that only the same app or other components using the same UserID can access this IntentService.
Start IntentService to start IntentService is similar to start Service, and we can input related parameters in Intent. For example:
package com.imooc.intentservicetest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = null;        for (int i = 0; i < 10; i++) {            intent = new Intent(this, MyIntentService.class);            intent.putExtra(xys,  + i);            startService(intent);        }    }}

We can see that we started 10 intentservices, And the execution result is as follows:
05-13 17:14:53.515  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent005-13 17:14:55.528  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent105-13 17:14:57.540  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent205-13 17:14:59.544  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent305-13 17:15:01.556  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent405-13 17:15:03.569  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent505-13 17:15:05.570  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent605-13 17:15:07.574  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent705-13 17:15:09.577  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent805-13 17:15:11.581  19991-20011/com.imooc.intentservicetest D/xys﹕ onHandleIntent9

Therefore, after the IntentService is started, it will generate a Worker Thread by default, and put these intentservices into the queue in sequence, each time one is retrieved for execution. After the execution is complete, this IntentService will automatically stop itself. When all intents are executed, the service will end and you do not need to manually end the service.

IntentService: If you want to modify the UI of the UIIntentService, you can only use Handler to implement it, or use the broadcast mechanism to notify you to modify the UI.
Differences between IntentService and AsyncTask
For the asynchronous UI update, IntentService uses the Serivce + handler or broadcast method, while AsyncTask is the thread + handler method. AsyncTask is more lightweight than IntentService. The running of a Thread is independent of the Activity. If the thread is not completed after the Activity ends, the Activity will no longer hold the reference of the thread. The Service cannot perform time-consuming operations in the onStart method, but can only be processed in sub-threads. When a new intent request comes, onStartCommond will queue it. After the first time-consuming operation is completed, the next time-consuming operation (onHandleIntent is called at this time) will be processed, and the onDestory will be automatically executed to destroy the IntengService.
 



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.