Simply put, Intentservice is a class that inherits from the service and processes asynchronous requests, and within Intentservice there is a worker thread to handle the time-consuming operation, starting the Intentservice in the same way as the traditional service, and When the task is finished, the Intentservice will stop automatically without the need for us to manually control it. 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.
Furthermore, 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.
Next let's take a look at how to use, write 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 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>
OK, and 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.
This is Intentservice, a class that allows us to handle business processes, which is a service, but smarter than service.
Android in Intentservice detailed