Service和IntentService的區別


Android中的Service是用於后台服務的,當應用程序被掛到后台的時候,問了保證應用某些組件仍然可以工作而引入了Service這個概念,那么這里面要強調的是Service不是獨立的進程,也不是獨立的線程,它是依賴於應用程序的主線程的,也就是說,在更多時候不建議在Service中編寫耗時的邏輯和操作,否則會引起ANR。

那么我們當我們編寫的耗時邏輯,不得不被service來管理的時候,就需要引入IntentService,IntentService是繼承Service的,那么它包含了Service的全部特性,當然也包含service的生命周期,那么與service不同的是,IntentService在執行onCreate操作的時候,內部開了一個線程,去你執行你的耗時操作。

這里我 需要解釋以下幾個方法,也許大家都已經很清楚了

Service中提供了一個方法:

  1. public int onStartCommand(Intent intent, int flags, int startId) {  
  2.      onStart(intent, startId);  
  3.      return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;  
  4.  }  


這個方法的具體含義是,當你的需要這個service啟動的時候,或者調用這個servcie的時候,那么這個方法首先是要被回調的。

同時IntentService中提供了這么一個方法:

  1. protected abstract void onHandleIntent(Intent intent);  

這是一個抽象方法,也就是說具體的實現需要被延伸到子類。

子類的聲明:

  1. public class ChargeService extends IntentService   

上面提到過IntentService是繼承Service的,那么這個子類也肯定繼承service,那么onHandleIntent()方法是什么時候被調用的呢?讓我們具體看IntentService的內部實現:

  1. private final class ServiceHandler extends Handler {  
  2.     public ServiceHandler(Looper looper) {  
  3.         super(looper);  
  4.     }  
  5.   
  6.     @Override  
  7.     public void handleMessage(Message msg) {  
  8.         onHandleIntent((Intent)msg.obj);  
  9.         stopSelf(msg.arg1);  
  10.     }  
  11. }  
  12.   
  13. /** 
  14.  * Creates an IntentService.  Invoked by your subclass's constructor. 
  15.  * 
  16.  * @param name Used to name the worker thread, important only for debugging. 
  17.  */  
  18. public IntentService(String name) {  
  19.     super();  
  20.     mName = name;  
  21. }  
  22.   
  23. /** 
  24.  * Sets intent redelivery preferences.  Usually called from the constructor 
  25.  * with your preferred semantics. 
  26.  * 
  27.  * <p>If enabled is true, 
  28.  * {@link #onStartCommand(Intent, int, int)} will return 
  29.  * {@link Service#START_REDELIVER_INTENT}, so if this process dies before 
  30.  * {@link #onHandleIntent(Intent)} returns, the process will be restarted 
  31.  * and the intent redelivered.  If multiple Intents have been sent, only 
  32.  * the most recent one is guaranteed to be redelivered. 
  33.  * 
  34.  * <p>If enabled is false (the default), 
  35.  * {@link #onStartCommand(Intent, int, int)} will return 
  36.  * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent 
  37.  * dies along with it. 
  38.  */  
  39. public void setIntentRedelivery(boolean enabled) {  
  40.     mRedelivery = enabled;  
  41. }  
  42.   
  43. @Override  
  44. public void onCreate() {  
  45.     // TODO: It would be nice to have an option to hold a partial wakelock  
  46.     // during processing, and to have a static startService(Context, Intent)  
  47.     // method that would launch the service & hand off a wakelock.  
  48.   
  49.     super.onCreate();  
  50.     HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");  
  51.     thread.start();  
  52.   
  53.     mServiceLooper = thread.getLooper();  
  54.     mServiceHandler = new ServiceHandler(mServiceLooper);  
  55. }  
  56.   
  57. @Override  
  58. public void onStart(Intent intent, int startId) {  
  59.     Message msg = mServiceHandler.obtainMessage();  
  60.     msg.arg1 = startId;  
  61.     msg.obj = intent;  
  62.     mServiceHandler.sendMessage(msg);  
  63. }  


在這里我們可以清楚的看到其實IntentService在執行onCreate的方法的時候,其實開了一個線程HandlerThread,並獲得了當前線程隊列管理的looper,並且在onStart的時候,把消息置入了消息隊列,

  1. @Override  
  2.        public void handleMessage(Message msg) {  
  3.            onHandleIntent((Intent)msg.obj);  
  4.            stopSelf(msg.arg1);  
  5.        }  

在消息被handler接受並且回調的時候,執行了onHandlerIntent方法,該方法的實現是子類去做的。

結論:

IntentService是通過Handler looper message的方式實現了一個多線程的操作,同時耗時操作也可以被這個線程管理和執行,同時不會產生ANR的情況。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM