Android asynchronous load full parsing Intentservice

Source: Internet
Author: User

Android asynchronous load full parsing intentservice what to do IntentserviceBefore we talked so much, asynchronous processing uses the king of Asynctask, and then the use of the thread, then the Intentservice is a ghost.In contrast to the two asynchronous loading methods we mentioned earlier, Intentservice has one of the biggest features, which is that--intentservice is not affected by most UI lifecycles, and it provides a more straightforward way for background threads to operate. However, the deficiencies of intentservice are mainly reflected in the following points:
    • You cannot interact directly with the UI. To show the results of his execution on the UI, it needs to be sent to the activity.
    • The work task queue is executed sequentially, and if a task is being executed in Intentservice, you send a task request again, and the task waits until the previous task has finished executing.
    • The task being performed cannot be interrupted.
Create IntentserviceWhen we create a class and inherit intentservice, we can basically generate the code shown below, through the IDE's hints:
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 of the worker thread, important only for debugging.     *    /Public Myintentservice (String name) {        super (name);    }    Public Myintentservice () {        super ("Myintentservice");    }    @Override    protected void onhandleintent (Intent Intent) {    }}

PS: It is important to note that we are prompted to generate a constructor with parameters, and we also need to define a parameterless construction method and invoke the constructor of a parameter. Therefore, a construction method with parameters can be deleted.
The most important thing is:
@Overrideprotected void Onhandleintent (Intent Intent) {}

In this method, we get the data from the intent and do the corresponding operation.
Ps:intentservice inherits from the service, but we do not need to manually invoke the service callback method such as Onstartcommand ().
Disclaimer IntentserviceWe need to make a statement to Intentservice in the Mainifest file:
<service android:name= ". Myintentservice "            android:exported=" false "/>

Similar to declaring a service, but does not need to declare <intent-filter> Because the activity that sends the task to Intentservice requires an explicit intent, the filter is not required. This also means that only the same app or other components that use the same userid will be able to access the Intentservice.
Start IntentserviceStarting the Intentservice is basically similar to starting the service, and we can pass in the relevant 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 < i++) {            intent = new Intent (this, myintentservice.class);            Intent.putextra ("XYs", "" "+ i);            StartService (intent);}}}    

As you can see, we start with 10 Intentservice, and the final 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

So, after starting Intentservice, it will generate a worker Thread by default, and put these intentservice in the queue, one at a time, and once executed, the Intentservice will automatically stop itself, When all intent are executed, the service is finished, and you do not need to end it manually.

Intentservice Modifying the UIIntentservice if you want to make changes to the UI, you can only do it through handler, or use the broadcast mechanism to notify the UI of the change.
The difference between Intentservice and Asynctask
    • For the asynchronous update UI, Intentservice uses Serivce+handler or broadcast, and Asynctask is the Thread+handler way.
    • Asynctask is a little more lightweight than intentservice.
    • The thread runs independently of the activity, and when the activity is finished, if the thread is not terminated, the activity will no longer hold a reference to that thread.
    • The service cannot perform time-consuming operations in the OnStart method, can only be processed in a child thread, and when a new intent request comes in, the line onstartcommond into the queue, and when the first time-consuming operation finishes, the next time-consuming operation is processed ( At this point, onhandleintent is called), and the automatic execution of the ondestory destroy Intengservice service is completed.

code address: Poke me poke me

my github
My video mu-class network


Android asynchronous load full parsing Intentservice

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.