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 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 intent = new Intent (MainActivity. this, IntentServiceSubclass. class); startService (intent );}});}}
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 ()");}}
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 ();}}}
<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>