MainActivity is as follows:
Package CC. testservice3; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. view; import android. view. view. onclicklistener; import android. widget. button;/*** demo Description: * use of intentservice ** demo details: * use service and intentservice here to simulate time-consuming tasks. * The Service will have an ANR error, and the intentservice will not ** cause description: * 1 service is not a dedicated process, * 2 service is not a new thread in the same process as its application, therefore, you cannot process time-consuming tasks in the service. ** intentservice inherits from the service, but a new thread is opened in onhandleintent () * to process tasks, therefore, ANR */public class mainactivity extends activity {private button mstartservicebutton; private button mstartintentservicebutton; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {// use the service to operate the time-consuming task mstartservicebutton = (button) findviewbyid (R. id. startservicebutton); mstartservicebutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {intent = new intent (mainactivity. this, servicesubclass. class); startservice (intent) ;}}); // use intentservice to operate the time-consuming task mstartintentservicebutton = (button) findviewbyid (R. id. startintentservicebutton); mstartintentservicebutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {intent = new intent (mainactivity. this, intentservicesubclass. class); startservice (intent );}});}}
ServiceSubclass:
Package cc. testservice3; import android. app. service; import android. content. intent; import android. OS. IBinder; public class ServiceSubclass extends Service {@ Overridepublic IBinder onBind (Intent arg0) {return null;} public void onCreate () {System. out. println ("---> Service onCreate ()") ;}@ Overridepublic void onStart (Intent intent, int startId) {super. onStart (intent, startId); System. out. println ("---> Service onStart ()") ;}@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {System. out. println ("---> Service onStartCommand ()"); // simulate time-consuming operations try {Thread. sleep (30*1000);} catch (Exception e) {e. printStackTrace ();} return super. onStartCommand (intent, flags, startId) ;}@ Overridepublic void onDestroy () {super. onDestroy (); System. out. println ("---> Service onDestroy ()");}}
IntentServiceSubclass:
Package cc. testservice3; import android. app. intentService; import android. content. intent;/*** Note: * Eclipse will prompt to generate a * constructor with the String parameter when inheriting from IntentService. after we generate the constructor according to the prompt, an error will be reported during the runtime: * java. lang. instantiationException ** solution: * Delete the constructor generated by the prompt and write a constructor with null parameters. * But in the construction method of this null parameter, execute super (""); **/public class IntentServiceSubclass extends IntentService {public IntentServiceSubclass () {super ("");} @ Overrideprotected void onHandleIntent (Intent intent) {// simulate time-consuming operation try {Thread. sleep (30*1000);} catch (Exception e) {e. printStackTrace ();}}}
Main. xml is as follows:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" Android: gravity = "center_horizontal"> <button Android: Id = "@ + ID/startservicebutton" Android: layout_width = "200dip" Android: layout_height = "150dip" Android: TEXT = "Start Service"/> <button Android: Id = "@ + ID/startintentservicebutton" Android: layout_width = "200dip" Android: layout_height = "150dip" Android: TEXT = "Start intentservice"/> </linearlayout>