Android --- 43 --- IntentService of Service, androidintent
Zookeeper
First, let's talk about two problems of the Service itself:
1. The Service does not start a new thread. The Service and its application are in the same process.
2. The Service is not a new thread, so it should not process time-consuming tasks in the Service.
IntentService makes up for these two shortcomings:
IntentService can process asynchronous requests.
IntentService uses a queue to manage the Intent request. When the client code initiates the IntentService through the Intent request, the IntentService
Adds the Intent to the queue, and starts a new worker thread to process the Intent. It is automatically stopped when it is completed, and you do not need to stop it manually.
IntentService features:
IntentService creates a separate worker thread to process all Intent requests.
IntentService creates a separate worker thread to process the Code implemented by the onHandleIntent method. Therefore, developers do not need to handle multithreading issues.
After all requests are processed, IntentService stops automatically. You do not need to call the stopSelf method to stop the Service.
Provides a default implementation for the onBind method of the Service. By default, this method returns null.
Provides the default implementation for the onStartCommand method of the Service. This implementation adds the Intent to the queue.
Therefore, when inheriting IntentService, The onBind onStartCommand method is not required. You only need to implement the onHandleIntent method.
Public class IntentServiceTest extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void startService (View source) {// create IntentIntent intent = new Intent (this, MyService. class); // start ServicestartService (intent);} public void startIntentService (View source) {// create IntentIntent intent = new Intent (this, MyIntentService. class); // start IntentServicestartService (intent );}}
In the preceding two time processing methods, MyService and MyIntentService are started respectively.
MyService. java:
Public class MyService extends Service {@ Overridepublic IBinder onBind (Intent intent) {return null ;}@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// execution of time-consuming tasks in this method may cause ANR (Application Not Responding) Exceptions long endTime = System. currentTimeMillis () + 20*1000; System. out. println ("onStart"); while (System. currentTimeMillis () <endTime) {synchronized (this) {try {wait (endTime-System. currentTimeMillis ();} catch (Exception e) {}} Toast. makeText (getApplicationContext (), "time-consuming task execution completed", 1 ). show (); System. out. println ("--- time-consuming task execution completed ---"); return START_STICKY ;}}
MyIntentService. java:
Public class MyIntentService extends IntentService {public MyIntentService () {super ("MyIntentService ");} // IntentService will use a separate thread to execute the code of this method @ Overrideprotected void onHandleIntent (Intent intent) {// any time-consuming tasks can be executed in this method, such as downloading files, the thread is paused for 20 seconds long endTime = System. currentTimeMillis () + 20*1000; System. out. println ("onStart"); while (System. currentTimeMillis () <endTime) {synchronized (this) {try {wait (endTime-System. currentTimeMillis ();} catch (Exception e) {}} System. out. println ("--- time-consuming task execution completed ---"); Toast. makeText (getApplicationContext (), "time-consuming task execution completed", 0 ). show ();}}