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 to handle 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 then the second executes 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 class Intentservicedemo extends Intentservice {public Intentservicedemo () {//must implement the construction method of the parent class Super ("Intentservicedemo"); } @Override Public IBinder onbind (Intent Intent) {System.out.println ("Onbind"); return Super.onbind (Intent); } @Override public void OnCreate () {System.out.println ("onCreate"); Super.oncreate (); } @Override public void OnStart (Intent Intent, int startid) {System.out.println ("OnStart"); Super.onstart (Intent, Startid); } @Override public int onstartcommand (Intent Intent, int flags, int startid) {System.out.println (" Onstartcommand "); Return Super.onstartcommand (Intent, flags, Startid); } @Override public void Setintentredelivery (Boolean enabled) {super.setintentredelivery (enabled); System.out.println ("Setintentredelivery"); } @Override Protected void Onhandleintent (Intent Intent) {//intent is sent from the activity, carries the identification parameters, performs different tasks according to the Parameters String 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 void OnDestroy () {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 class Testactivity extends activity { /** called when the activity is first created. */ @Override publi c void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Can be started multiple times, each time it is started, a new work thread is created, but the Intentservice instance always has only one //operation 1 Intent startserviceintent = new Intent ("Com.test.intentservice"); Bundle bundle = new bundle (); Bundle.putstring ("param", "oper1"); Startserviceintent.putextras (bundle); StartService (startserviceintent); Operation 2 Intent startServiceIntent2 = new Intent ("Com.test.intentservice"); Bundle Bundle2 = new bundle (); 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.
<service android:name= ". Intentservicedemo "> <intent-filter > <action 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