組件之IntentService詳解

一、IntentService解析

(1)IntentService的特點

  • IntentService自帶一個工作線程,當(dāng)我們的Service需要做一些可能會阻塞主線程的工作的時候可以考慮使用IntentService。
  • IntentService通過在onCreate中創(chuàng)建HandlerThread線程、在onStartCommand發(fā)送消息到IntentService內(nèi)部類ServiceHandler中,在handleMessage中調(diào)用onHandleIntent在子線程完成工作。
  • 當(dāng)我們通過startService多次啟動IntentService會產(chǎn)生多個Message消息。由于IntentService只持有一個工作線程,所以每次onHandleIntent只能處理一個Message消息,IntentService不能并行的執(zhí)行多個intent,只能一個一個的按照先后順序完成,當(dāng)所有消息完成的時候IntentService就銷毀了,會執(zhí)行onDestroy回調(diào)方法。
    (2)IntentService實現(xiàn)類
public class DownLoadIntentService extends IntentService {
    
    public DownLoadIntentService(String name) {
        super(name);
    }
    //運行在主線程
    @Override
    public void onCreate() {
        super.onCreate();
    }

    //運行在主線程
    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    //在子線程中執(zhí)行
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

    }
}

組件之IntentService源碼解析

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在手機上去實現(xiàn)一些動畫效果算是件比較炫酷的事情,因此Android系統(tǒng)在一開始的時候就給我們提供了兩種實現(xiàn)動畫效果...
    Ten_Minutes閱讀 3,933評論 3 11
  • 參考于:http://blog.csdn.net/guolin_blog/article/details/4353...
    墨染書閱讀 3,071評論 0 2
  • 走山的丈夫 石順江 (1083) 20年前,紅娟愛上了來小城打工的小伙兒林強。當(dāng)她將心思告訴父母后,父親反...
    90b5fc5ecdb2閱讀 410評論 0 0

友情鏈接更多精彩內(nèi)容