Here, intentservice uses the queue method to add the requested intent to the queue, and then starts a worker thread to process the intent in the queue, for Asynchronous startservice requests, intentservice will process the second request after processing the completed one. Each request will be processed in a separate worker thread and will not block the main thread of the application, here we provide an idea. If a time-consuming operation is used, it is better to use intentservice to process time-consuming operations than to enable a new thread in the service. The following is a small example:
1. Service:
- PackageCom. zhf. Service;
- ImportAndroid. App. Service;
- ImportAndroid. content. intent;
- ImportAndroid. OS. ibinder;
- Public ClassMyserviceExtendsService {
- @ Override
- Public VoidOncreate (){
- Super. Oncreate ();
- }
- @ Override
- Public VoidOnstart (intent,IntStartid ){
- Super. Onstart (intent, startid );
- // After testing, the service cannot perform time-consuming operations. You must manually start a working thread to process time-consuming operations.
- System. Out. println ("onstart ");
- Try{
- Thread. Sleep (20000 );
- }Catch(Interruptedexception e ){
- E. printstacktrace ();
- }
- System. Out. println ("sleep ends ");
- }
- @ Override
- PublicIbinder onbind (intent ){
- Return Null;
- }
- }
|
2. intentservice:
- PackageCom. zhf. Service;
- ImportAndroid. App. intentservice;
- ImportAndroid. content. intent;
- Public ClassMyintentserviceExtendsIntentservice {
- PublicMyintentservice (){
- Super("Yyyyyyyyy ");
- }
- @ Override
- Protected VoidOnhandleintent (intent ){
- // Tested, intentservice can perform time-consuming operations.
- // Intentservice adds the requested intent to the queue by queue, and starts a worker thread to process the intent in the queue.
- // For asynchronous startservice requests, intentservice completes processing and then processes the second request.
- System. Out. println ("onstart ");
- Try{
- Thread. Sleep (20000 );
- }Catch(Interruptedexception e ){
- E. printstacktrace ();
- }
- System. Out. println ("sleep ends ");
- }
- }
|
Test main program:
- PackageCom. zhf. Service;
- ImportAndroid. App. activity;
- ImportAndroid. content. intent;
- ImportAndroid. OS. Bundle;
- Public ClassServicedemoactivityExtendsActivity {
- /** Called when the activity is first created .*/
- @ Override
- Public VoidOncreate (bundle savedinstancestate ){
- Super. Oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Startservice (NewIntent (This, Myservice.Class); // The main interface is blocked, and application not responding will eventually appear.
- // Start intentservice twice in a row and the application will not be blocked, and the heaviest thing is that the second request will run after the first request ends (this confirms that intentservice uses a separate thread to process only one request from the queue at a time)
- Startservice (NewIntent (This, Myintentservice.Class));
- Startservice (NewIntent (This, Myintentservice.Class));
- }
- }
|