In Android development, we may encounter such a business need, a task is divided into several sub-tasks, sub-tasks executed sequentially, after the completion of all subtasks, the task is only successful. Then, it is possible to do this with a few sub-threading sequence execution, but each thread must be manually controlled, and another child thread will be opened after one of the sub-threads has finished executing. Or, put them all in one thread and let them execute sequentially. This can be done, however, if this is a background task, you have to put in the service, because the service and activity are peer, so to perform time-consuming tasks, you have to open a sub-thread in the service to execute . So, is there a simple way to deal with this process, the answer is Intentservice.
What is Intentservice,Intentservice is a class that inherits from the service and processes an asynchronous request, and within Intentservice there is a worker thread that handles the time-consuming Operation , Starting the Intentservice is the same as starting a traditional service, and when the task is finished, Intentservice will stop automatically without the need for manual control. In addition, you can start intentservice multiple times, and each time-consuming operation is performed in the Onhandleintent callback method of Intentservice in the form of a work queue, and only one worker thread is executed at a time, and the second is executed after the first one. And so on
All requests are in one single thread and do not block the application's main thread (UI Thread), and only one request is processed at a time.
So what are the benefits of using Intentservice? First, we eliminate the hassle of manual threads in the service, and second, we don't have to stop the service manually when the operation is complete.
Code
Next let's take a look at how to use, I wrote a demo to simulate two time-consuming operations, Operation1 and Operation2, first execute must wait 1 execution to execute:
New project, create a new class that inherits Intentservice, I'm Intentservicedemo.java here.
Public classIntentservicedemoextendsIntentservice { PublicIntentservicedemo () {//you must implement the parent class's construction method Super("Intentservicedemo"); } @Override Publicibinder onbind (Intent Intent) {System.out.println ("Onbind"); return Super. Onbind (Intent); } @Override Public voidonCreate () {System.out.println ("OnCreate"); Super. OnCreate (); } @Override Public voidOnStart (Intent Intent,intStartid) {System.out.println ("OnStart"); Super. OnStart (Intent, Startid); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {System.out.println ("Onstartcommand"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidSetintentredelivery (Booleanenabled) { Super. Setintentredelivery (enabled); System.out.println ("Setintentredelivery"); } @Overrideprotected voidonhandleintent (Intent Intent) {//intent is sent from the activity, carry the identification parameters, according to different parameters to perform different tasksString action = Intent.getextras (). getString ("param"); if(Action.equals ("Oper1") {System.out.println ("Operation1"); }Else if(Action.equals ("Oper2") {System.out.println ("Operation2"); } Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }} @Override Public voidOnDestroy () {System.out.println ("OnDestroy"); Super. OnDestroy (); } }
I've printed out the life cycle method, and then we'll see how it performs. Next is the activity, which starts the intentservice in the activity:
Public classTestactivityextendsActivity {/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //can be started multiple times, each time you start, a new work thread is created, but the Intentservice instance always has only one//Operation 1Intent startserviceintent =NewIntent ("Com.test.intentservice"); Bundle Bundle=NewBundle (); Bundle.putstring ("Param", "oper1"); Startserviceintent.putextras (bundle); StartService (startserviceintent); //Operation 2Intent StartServiceIntent2 =NewIntent ("Com.test.intentservice"); Bundle Bundle2=NewBundle (); Bundle2.putstring ("Param", "oper2"); Startserviceintent2.putextras (Bundle2); StartService (StartServiceIntent2); } }
Finally, do not forget to configure the service, because it inherits from the service, so, it is a service, must be configured, otherwise it will not work, I just forget, the results half a day did not respond.
<android:name= ". Intentservicedemo "> <> < Android:name = "Com.test.intentservice" /> </ Intent-filter > </ Service >
Finally, take a look at the results:
As you can see from the results, the OnCreate method executes only once, and the Onstartcommand and OnStart methods execute two times, opening two work Thread, which confirms what was said earlier, starts several times, but the Intentservice instance has only one, This is the same as the traditional service. Operation1 also before Operation2 printing, and I let two operations between the pause 2s, and finally OnDestroy destroyed Intentservice.
I'm the dividing line of the king of the land Tiger.
Reference: http://blog.csdn.net/ryantang03/article/details/8146154
Android interview, the principle and use of intentservice