Android IntentService詳解及使用實(shí)例
Android IntentService詳解
一、IntentService簡(jiǎn)介
IntentService是Service的子類,比普通的Service增加了額外的功能。先看Service本身存在兩個(gè)問題:
- Service不會(huì)專門啟動(dòng)一條單獨(dú)的進(jìn)程,Service與它所在應(yīng)用位于同一個(gè)進(jìn)程中;
- Service也不是專門一條新線程,因此不應(yīng)該在Service中直接處理耗時(shí)的任務(wù);
二、IntentService特征
- 會(huì)創(chuàng)建獨(dú)立的worker線程來處理所有的Intent請(qǐng)求;
- 會(huì)創(chuàng)建獨(dú)立的worker線程來處理onHandleIntent()方法實(shí)現(xiàn)的代碼,無需處理多線程問題;
- 所有請(qǐng)求處理完成后,IntentService會(huì)自動(dòng)停止,無需調(diào)用stopSelf()方法停止Service;
- 為Service的onBind()提供默認(rèn)實(shí)現(xiàn),返回null;
- 為Service的onStartCommand提供默認(rèn)實(shí)現(xiàn),將請(qǐng)求Intent添加到隊(duì)列中;
三、使用步驟(詳情參考Service項(xiàng)目)
繼承IntentService類,并重寫onHandleIntent()方法即可;
MainActivity.Java文件
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startService(View source) { // 創(chuàng)建所需要啟動(dòng)的Service的Intent Intent intent = new Intent(this, MyService.class); startService(intent); } public void startIntentService(View source) { // 創(chuàng)建需要啟動(dòng)的IntentService的Intent Intent intent = new Intent(this, MyIntentService.class); startService(intent); } }
MyIntentService.java文件
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { // IntentService會(huì)使用單獨(dú)的線程來執(zhí)行該方法的代碼 // 該方法內(nèi)執(zhí)行耗時(shí)任務(wù),比如下載文件,此處只是讓線程等待20秒 long endTime = System.currentTimeMillis() + 20 * 1000; System.out.println("onStart"); while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } System.out.println("----耗時(shí)任務(wù)執(zhí)行完成---"); } }
MyService.java文件
public class MyService extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 該方法內(nèi)執(zhí)行耗時(shí)任務(wù)可能導(dǎo)致ANR(Application Not Responding)異常 long endTime = System.currentTimeMillis() + 20 * 1000; System.out.println("onStart"); while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } System.out.println("----耗時(shí)任務(wù)執(zhí)行完成---"); return START_STICKY; } }
運(yùn)行上述代碼,啟動(dòng)MyIntentService的會(huì)使用單獨(dú)的worker線程,因此不會(huì)阻塞前臺(tái)的UI線程;而MyService會(huì)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android Service類與生命周期詳細(xì)介紹
- 詳解Android中的Service
- Android 如何保證service在后臺(tái)不被kill
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實(shí)現(xiàn)微信自動(dòng)向附近的人打招呼(AccessibilityService)
- Android AccessibilityService實(shí)現(xiàn)微信搶紅包插件
- Android Service中使用Toast無法正常顯示問題的解決方法
- Android基于service實(shí)現(xiàn)音樂的后臺(tái)播放功能示例
- Android Service的啟動(dòng)過程分析
相關(guān)文章
100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼
這篇文章主要介紹了100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android?WebView的使用與后退鍵處理詳細(xì)討論
在android開發(fā)中我們有時(shí)候根據(jù)項(xiàng)目的需求多少會(huì)加載一些webview,加載webview,我們有時(shí)候會(huì)根據(jù)UI來自定義返回鍵,下面這篇文章主要給大家介紹了關(guān)于Android?WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04使用RecyclerView添加Header和Footer的方法
RecyclerView雖然作為L(zhǎng)istView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時(shí)會(huì)經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個(gè)方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹2016-03-03Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
這篇文章將借助開源庫?MLKit?實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功,該庫的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測(cè)等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下2023-08-08android列表控件實(shí)現(xiàn)展開、收縮功能
這篇文章主要為大家詳細(xì)介紹了android支持展開/收縮功能的列表控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11android開發(fā)教程之系統(tǒng)資源的使用方法 android資源文件
這篇文章主要介紹了android中的系統(tǒng)資源的使用方法,包括顏色資源 、字符串資源、尺寸資源、XML資源文件,需要的朋友可以參考下2014-02-02Android事件分發(fā)機(jī)制(下) View的事件處理
這篇文章主要介紹了Android事件分發(fā)機(jī)制下篇, View的事件處理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01