Comparison between Android service and intentservice

Source: Internet
Author: User

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:

 

 
  1. PackageCom. zhf. Service;
  2. ImportAndroid. App. Service;
  3. ImportAndroid. content. intent;
  4. ImportAndroid. OS. ibinder;
  5. Public ClassMyserviceExtendsService {
  6. @ Override
  7. Public VoidOncreate (){
  8. Super. Oncreate ();
  9. }
  10. @ Override
  11. Public VoidOnstart (intent,IntStartid ){
  12. Super. Onstart (intent, startid );
  13. // After testing, the service cannot perform time-consuming operations. You must manually start a working thread to process time-consuming operations.
  14. System. Out. println ("onstart ");
  15. Try{
  16. Thread. Sleep (20000 );
  17. }Catch(Interruptedexception e ){
  18. E. printstacktrace ();
  19. }
  20. System. Out. println ("sleep ends ");
  21. }
  22. @ Override
  23. PublicIbinder onbind (intent ){
  24. Return Null;
  25. }
  26. }

 

2. intentservice:

 

 
  1. PackageCom. zhf. Service;
  2. ImportAndroid. App. intentservice;
  3. ImportAndroid. content. intent;
  4. Public ClassMyintentserviceExtendsIntentservice {
  5. PublicMyintentservice (){
  6. Super("Yyyyyyyyy ");
  7. }
  8. @ Override
  9. Protected VoidOnhandleintent (intent ){
  10. // Tested, intentservice can perform time-consuming operations.
  11. // Intentservice adds the requested intent to the queue by queue, and starts a worker thread to process the intent in the queue.
  12. // For asynchronous startservice requests, intentservice completes processing and then processes the second request.
  13. System. Out. println ("onstart ");
  14. Try{
  15. Thread. Sleep (20000 );
  16. }Catch(Interruptedexception e ){
  17. E. printstacktrace ();
  18. }
  19. System. Out. println ("sleep ends ");
  20. }
  21. }

 

Test main program:

 

 
  1. PackageCom. zhf. Service;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. content. intent;
  4. ImportAndroid. OS. Bundle;
  5. Public ClassServicedemoactivityExtendsActivity {
  6. /** Called when the activity is first created .*/
  7. @ Override
  8. Public VoidOncreate (bundle savedinstancestate ){
  9. Super. Oncreate (savedinstancestate );
  10. Setcontentview (R. layout. Main );
  11. Startservice (NewIntent (This, Myservice.Class); // The main interface is blocked, and application not responding will eventually appear.
  12. // 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)
  13. Startservice (NewIntent (This, Myintentservice.Class));
  14. Startservice (NewIntent (This, Myintentservice.Class));
  15. }
  16. }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.