Android之Service與IntentService的比較

來源:互聯網
上載者:User

稍微翻譯理一理,這裡主要是說IntentService使用隊列的方式將請求的Intent排入佇列,然後開啟一個worker thread(線程)來處理隊列中的Intent,對於非同步startService請求,IntentService會處理完成一個之後再處理第二個,每一個請求都會在一個單獨的worker thread中處理,不會阻塞應用程式的主線程,這裡就給我們提供了一個思路,如果有耗時的操作與其在Service裡面開啟新線程還不如使用IntentService來處理耗時操作。下面給一個小例子:

 

 

1.Service:

 

 
  1. package com.zhf.service;  
  2.   
  3. import Android.app.Service;  
  4. import Android.content.Intent;  
  5. import Android.os.IBinder;  
  6.   
  7. public class MyService extends Service {  
  8.   
  9.     @Override  
  10.     public void onCreate() {  
  11.         super.onCreate();  
  12.     }  
  13.       
  14.     @Override  
  15.     public void onStart(Intent intent, int startId) {  
  16.         super.onStart(intent, startId);  
  17.         //經測試,Service裡面是不能進行耗時的操作的,必須要手動開啟一個背景工作執行緒來處理耗時操作   
  18.         System.out.println("onStart");  
  19.         try {  
  20.             Thread.sleep(20000);  
  21.         } catch (InterruptedException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         System.out.println("睡眠結束");  
  25.     }  
  26.       
  27.     @Override  
  28.     public IBinder onBind(Intent intent) {  
  29.         return null;  
  30.     }  
  31. }  

 

2.IntentService:

 

 
  1. package com.zhf.service;  
  2.   
  3. import Android.app.IntentService;  
  4. import Android.content.Intent;  
  5.   
  6. public class MyIntentService extends IntentService {  
  7.   
  8.     public MyIntentService() {  
  9.         super("yyyyyyyyyyy");  
  10.     }  
  11.   
  12.     @Override  
  13.     protected void onHandleIntent(Intent intent) {  
  14.         // 經測試,IntentService裡面是可以進行耗時的操作的   
  15.         //IntentService使用隊列的方式將請求的Intent排入佇列,然後開啟一個worker thread(線程)來處理隊列中的Intent   
  16.         //對於非同步startService請求,IntentService會處理完成一個之後再處理第二個   
  17.         System.out.println("onStart");  
  18.         try {  
  19.             Thread.sleep(20000);  
  20.         } catch (InterruptedException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         System.out.println("睡眠結束");  
  24.     }  
  25. }  

 

測試主程式:

 

 
  1. package com.zhf.service;  
  2.   
  3. import Android.app.Activity;  
  4. import Android.content.Intent;  
  5. import Android.os.Bundle;  
  6.   
  7. public class ServiceDemoActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         startService(new Intent(this,MyService.class));//主介面阻塞,最終會出現Application not responding   
  14.         //連續兩次啟動IntentService,會發現應用程式不會阻塞,而且最重的是第二次的請求會再第一個請求結束之後運行(這個證實了IntentService採用單獨的線程每次只從隊列中拿出一個請求進行處理)   
  15.         startService(new Intent(this,MyIntentService.class));  
  16.         startService(new Intent(this,MyIntentService.class));  
  17.     }  
  18. }  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.