最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android IntentService詳解及使用實(shí)例

 更新時(shí)間:2017年03月01日 16:47:56   投稿:lqh  
這篇文章主要介紹了Android IntentService詳解及使用實(shí)例的相關(guān)資料,需要的朋友可以參考下

Android IntentService詳解

一、IntentService簡介

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請求; 
  • 會(huì)創(chuàng)建獨(dú)立的worker線程來處理onHandleIntent()方法實(shí)現(xiàn)的代碼,無需處理多線程問題; 
  • 所有請求處理完成后,IntentService會(huì)自動(dòng)停止,無需調(diào)用stopSelf()方法停止Service; 
  • 為Service的onBind()提供默認(rèn)實(shí)現(xiàn),返回null; 
  • 為Service的onStartCommand提供默認(rèn)實(shí)現(xiàn),將請求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ì)本站的支持!

相關(guān)文章

  • android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離

    android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離

    這篇文章主要為大家詳細(xì)介紹了android采用FFmpeg實(shí)現(xiàn)音視頻合成與分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android實(shí)現(xiàn)帶簽到贏積分功能的日歷

    Android實(shí)現(xiàn)帶簽到贏積分功能的日歷

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶簽到贏積分功能的日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Kotlin空安全空類型淺談

    Kotlin空安全空類型淺談

    這篇文章主要為大家介紹了Kotlin空安全空類型的實(shí)用技巧淺談,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android如何為按鍵添加聲音

    Android如何為按鍵添加聲音

    這篇文章主要告訴大家Android為按鍵添加聲音的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • android 設(shè)置圓角圖片實(shí)現(xiàn)代碼

    android 設(shè)置圓角圖片實(shí)現(xiàn)代碼

    在android應(yīng)用開發(fā)中,可能是美化需要,圖片需要處理成圓角,本文將給出實(shí)現(xiàn)代碼,開發(fā)中的遇到此問題的朋友可以參考下
    2012-11-11
  • Android自定義View實(shí)現(xiàn)動(dòng)畫效果詳解

    Android自定義View實(shí)現(xiàn)動(dòng)畫效果詳解

    這篇文章主要為大家詳細(xì)介紹了Android如何通過自定義View實(shí)現(xiàn)動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-02-02
  • Android基礎(chǔ)之獲取LinearLayout的寬高

    Android基礎(chǔ)之獲取LinearLayout的寬高

    LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來排列所有的widgets或者其他的containers,超過邊界時(shí),某些控件將缺失或消失。有的時(shí)候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • Android開發(fā)中ViewPager實(shí)現(xiàn)多頁面切換效果

    Android開發(fā)中ViewPager實(shí)現(xiàn)多頁面切換效果

    ViewPager用于實(shí)現(xiàn)多頁面的切換效果,該類存在于Google的兼容包里面,所以在引用時(shí)記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文
    2016-11-11
  • Android常見XML轉(zhuǎn)義字符(總結(jié))

    Android常見XML轉(zhuǎn)義字符(總結(jié))

    下面小編就為大家?guī)硪黄狝ndroid常見XML轉(zhuǎn)義字符(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 使用PlatformView將?Android?控件view制作成Flutter插件

    使用PlatformView將?Android?控件view制作成Flutter插件

    這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評(píng)論

丽水市| 阿坝县| 巩留县| 济阳县| 天气| 衢州市| 定兴县| 遵义市| 阳曲县| 龙胜| 东城区| 井陉县| 陈巴尔虎旗| 广州市| 连城县| 夏津县| 宣恩县| 武穴市| 崇信县| 花莲市| 若尔盖县| 江孜县| 九龙城区| 藁城市| 宁陕县| 广水市| 巴彦淖尔市| 霍山县| 雷州市| 琼海市| 七台河市| 宁武县| 石棉县| 睢宁县| 梅州市| 广水市| 天等县| 林周县| 安国市| 年辖:市辖区| 湖口县|