亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入剖析Android系統(tǒng)中Service和IntentService的區(qū)別

 更新時間:2016年04月22日 15:31:42   作者:matrix_xu  
這篇文章主要介紹了Android系統(tǒng)中Service和IntentService的區(qū)別,與普通的服務相比,IntentService可以開啟單獨的線程來處理intent請求,需要的朋友可以參考下

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

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

service本身存在兩個問題:

(1)service不會專門啟動一條單獨的進程,service與它所在的應用位于同一個進程。

(2)service也不是專門新的一條線程,不應該在service中處理耗時的操作。

IntentService很好的彌補了這一點:

(1)IntentService會創(chuàng)建單獨的worker線程來處理所有的intent請求。

(2)IntentService會創(chuàng)建單獨的worker線程來處理onHandleIntent()方法實現(xiàn)的代碼。

(3)當所有的請求處理完之后,IntentService會自動停止。

(4)為Service的OnBind()方法提供了默認的實現(xiàn),返回null。

(5)為service的onStartCommand()方法提供了默認的實現(xiàn),該實現(xiàn)會將請求intent添加到隊列中。

所以對IntentService的使用就是:繼承IntentService,重寫onHandleIntent()方法即可。

tips:
(1)Intentservice也必須在manifest中聲明。
(2)實現(xiàn)類的構(gòu)造方法必須實現(xiàn)默認的構(gòu)造方法。

這里我 需要解釋以下幾個方法,也許大家都已經(jīng)很清楚了,不過為了拋磚引玉,我還是要提一嘴。

Service中提供了一個方法:

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

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

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

protected abstract void onHandleIntent(Intent intent); 

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

子類的聲明:

public class ChargeService extends IntentService  

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

private final class ServiceHandler extends Handler { 
  public ServiceHandler(Looper looper) { 
    super(looper); 
  } 
 
  @Override 
  public void handleMessage(Message msg) { 
    onHandleIntent((Intent)msg.obj); 
    stopSelf(msg.arg1); 
  } 
} 
 
/** 
 * Creates an IntentService. Invoked by your subclass's constructor. 
 * 
 * @param name Used to name the worker thread, important only for debugging. 
 */ 
public IntentService(String name) { 
  super(); 
  mName = name; 
} 
 
/** 
 * Sets intent redelivery preferences. Usually called from the constructor 
 * with your preferred semantics. 
 * 
 * <p>If enabled is true, 
 * {@link #onStartCommand(Intent, int, int)} will return 
 * {@link Service#START_REDELIVER_INTENT}, so if this process dies before 
 * {@link #onHandleIntent(Intent)} returns, the process will be restarted 
 * and the intent redelivered. If multiple Intents have been sent, only 
 * the most recent one is guaranteed to be redelivered. 
 * 
 * <p>If enabled is false (the default), 
 * {@link #onStartCommand(Intent, int, int)} will return 
 * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent 
 * dies along with it. 
 */ 
public void setIntentRedelivery(boolean enabled) { 
  mRedelivery = enabled; 
} 
 
@Override 
public void onCreate() { 
  // TODO: It would be nice to have an option to hold a partial wakelock 
  // during processing, and to have a static startService(Context, Intent) 
  // method that would launch the service & hand off a wakelock. 
 
  super.onCreate(); 
  HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); 
  thread.start(); 
 
  mServiceLooper = thread.getLooper(); 
  mServiceHandler = new ServiceHandler(mServiceLooper); 
} 
 
@Override 
public void onStart(Intent intent, int startId) { 
  Message msg = mServiceHandler.obtainMessage(); 
  msg.arg1 = startId; 
  msg.obj = intent; 
  mServiceHandler.sendMessage(msg); 
} 

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

@Override 
    public void handleMessage(Message msg) { 
      onHandleIntent((Intent)msg.obj); 
      stopSelf(msg.arg1); 
    } 

在消息被handler接受并且回調(diào)的時候,執(zhí)行了onHandlerIntent方法,該方法的實現(xiàn)是子類去做的。

結(jié)論:

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

相關(guān)文章

最新評論