Android IntentService詳解及使用實(shí)例
Android IntentService詳解
一、IntentService簡(jiǎn)介
IntentService是Service的子類(lèi),比普通的Service增加了額外的功能。先看Service本身存在兩個(gè)問(wèn)題:
- Service不會(huì)專門(mén)啟動(dòng)一條單獨(dú)的進(jìn)程,Service與它所在應(yīng)用位于同一個(gè)進(jìn)程中;
- Service也不是專門(mén)一條新線程,因此不應(yīng)該在Service中直接處理耗時(shí)的任務(wù);
二、IntentService特征
- 會(huì)創(chuàng)建獨(dú)立的worker線程來(lái)處理所有的Intent請(qǐng)求;
- 會(huì)創(chuàng)建獨(dú)立的worker線程來(lái)處理onHandleIntent()方法實(shí)現(xiàn)的代碼,無(wú)需處理多線程問(wèn)題;
- 所有請(qǐng)求處理完成后,IntentService會(huì)自動(dòng)停止,無(wú)需調(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類(lèi),并重寫(xiě)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ú)的線程來(lái)執(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類(lèi)與生命周期詳細(xì)介紹
- 詳解Android中的Service
- Android 如何保證service在后臺(tái)不被kill
- android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息
- Android實(shí)現(xiàn)微信自動(dòng)向附近的人打招呼(AccessibilityService)
- Android AccessibilityService實(shí)現(xiàn)微信搶紅包插件
- Android Service中使用Toast無(wú)法正常顯示問(wèn)題的解決方法
- Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能示例
- Android Service的啟動(dòng)過(guò)程分析
相關(guān)文章
android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離
這篇文章主要為大家詳細(xì)介紹了android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android實(shí)現(xiàn)帶簽到贏積分功能的日歷
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶簽到贏積分功能的日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
在android應(yīng)用開(kāi)發(fā)中,可能是美化需要,圖片需要處理成圓角,本文將給出實(shí)現(xiàn)代碼,開(kāi)發(fā)中的遇到此問(wèn)題的朋友可以參考下2012-11-11
Android自定義View實(shí)現(xiàn)動(dòng)畫(huà)效果詳解
這篇文章主要為大家詳細(xì)介紹了Android如何通過(guò)自定義View實(shí)現(xiàn)動(dòng)畫(huà)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
Android基礎(chǔ)之獲取LinearLayout的寬高
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來(lái)排列所有的widgets或者其他的containers,超過(guò)邊界時(shí),某些控件將缺失或消失。有的時(shí)候,我們需要想獲取LinearLayout寬高,下面通過(guò)這篇文章來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
Android開(kāi)發(fā)中ViewPager實(shí)現(xiàn)多頁(yè)面切換效果
ViewPager用于實(shí)現(xiàn)多頁(yè)面的切換效果,該類(lèi)存在于Google的兼容包里面,所以在引用時(shí)記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文2016-11-11
Android常見(jiàn)XML轉(zhuǎn)義字符(總結(jié))
下面小編就為大家?guī)?lái)一篇Android常見(jiàn)XML轉(zhuǎn)義字符(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

