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