Android asynchronous loading full parse Intentservice what IntentserviceWe have said so much earlier that asynchronous processing uses the asynctask of the King James. Then the thread that is used, then this intentservice is a what ghost.
Relative to the two types of asynchronous loading that we mentioned earlier. Intentservice has one of the biggest features. Is that--intentservice is not affected by most of the UI life cycle. It provides a more straightforward way for a background thread to operate. But the intentservice of the main body is now the following points:
- Not able to interact directly with the UI. In order to put him running the result body is now on the UI. Need to be sent to activity.
- The work task queue runs sequentially, assuming that a task is running in Intentservice, and then you send a task request, and the task waits until the previous task runs to completion.
- The running task cannot be interrupted.
Create IntentserviceAfter we have created a class and inherited Intentservice, we are basically able to generate code such as the following as we can see 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 the system will prompt us to generate a constructor with a number of parameters, in addition. We also need to define a non-reference construction method and invoke a constructor of the parameter. So. A construction method with a number of parameters can actually 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 inherit from service. However, we do not need to manually invoke the service callback method such as Onstartcommand ().
Statement Intentservice We need to make a statement to Intentservice in the Mainifest document:
<service android:name= ". Myintentservice " android:exported=" false "/>
Similar to the declaration of a service, but does not need to declare <intent-filter> Because the activity that sends the task to intentservice needs to use the explicit intent, therefore does not need the filter. This also means that only the components in the same app or other use the same userid can access this intentservice.
Starting the Intentservice startup Intentservice is basically similar to starting the service. And we are able to pass in the relevant parameters in the intent. Like what:
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);}}}
Able to see, we start with 10 Intentservice, and the last running results such as the following:
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 run when the run is complete. This intentservice will voluntarily stop himself. When all intent are complete, the service is over, and you do not need to end it manually.
Intentservice UI ChangesIntentservice assumes that UI changes can be made only through handler, or by using a 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 when the activity is finished. Assuming the thread is not terminated, the activity will no longer hold a reference to that thread.
- The service cannot run a time-consuming operation in the OnStart method, it 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 ( Call Onhandleintent at this point). Have run out of their own initiative to run ondestory destroy Intengservice services.
code address: Poke me poke me
my github
My video mu-class network
Android asynchronous load full parsing Intentservice