A) mainactivity part of the code
1 PackageCom.example.cms.intentservice;2 3 Importandroid.content.Intent;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.Button;8 9 Public classMainactivityextendsappcompatactivity {Ten PrivateButton StartService; One PrivateButton Startintentservice; A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Startservice=(Button) Findviewbyid (r.id.service); -Startintentservice=(Button) Findviewbyid (r.id.intentservice); +Startservice.setonclicklistener (NewView.onclicklistener () { - @Override + Public voidOnClick (View v) { A StartService (); at } - }); -Startintentservice.setonclicklistener (NewView.onclicklistener () { - @Override - Public voidOnClick (View v) { - Startintentservice (); in } - }); to } + Public voidStartService () { -Intent Intent =NewIntent ( This, MyService.class); the StartService (intent); * } $ Public voidStartintentservice () {Panax NotoginsengIntent intent=NewIntent ( This, Myintentservice.class); - StartService (intent); the } +}
B) MyService part of the code
1 PackageCom.example.cms.intentservice;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.IBinder;6 Importandroid.support.annotation.Nullable;7 8 /**9 * Created by CMS on 2016/3/28.Ten */ One Public classMyServiceextendsService { A @Nullable - @Override - Publicibinder onbind (Intent Intent) { the return NULL; - } - - @Override + Public intOnstartcommand (Intent Intent,intFlagsintStartid) { - //use thread pauses to simulate time-consuming tasks pausing for 20 seconds + //Normal service execution will block the main thread so executing this method will cause a ANR exception A LongEndtime=system.currenttimemillis () +20*1000; atSystem.out.println ("OnStart"); - while(System.currenttimemillis () <endTime) { - synchronized( This){ - Try { -Wait (endtime-System.currenttimemillis ()); -}Catch(interruptedexception e) { in e.printstacktrace (); - } to } + } -SYSTEM.OUT.PRINTLN ("---time-consuming task execution complete----"); the returnStart_sticky; * } $}
C) Myintentservice part of the code
PackageCom.example.cms.intentservice;ImportAndroid.app.IntentService;Importandroid.content.Intent;ImportAndroid.content.IntentSender;/*** Created by CMS on 2016/3/28.*/ Public classMyintentserviceextendsIntentservice { PublicMyintentservice () {Super("Myintentservice"); } @Overrideprotected voidonhandleintent (Intent Intent) {//Intentservice will use a separate thread to execute the code for this method//Does not block the main thread so there is no ABR exception LongEndtime=system.currenttimemillis () +20*1000; System.out.println ("Onhandleintent"); while(System.currenttimemillis () <endTime) { synchronized( This){ Try{Wait (endTime-System.currenttimemillis ()); } Catch(interruptedexception e) {e.printstacktrace (); }}} System.out.println ("Time-consuming task execution complete"); }}
Android Self-study road--service and Intentservice