欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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ì)本站的支持!

相關(guān)文章

  • Android控件系列之Shape使用方法

    Android控件系列之Shape使用方法

    Android控件系列之Shape使用方法,需要的朋友可以參考一下
    2013-05-05
  • 100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼

    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-09
  • Android?WebView的使用與后退鍵處理詳細(xì)討論

    Android?WebView的使用與后退鍵處理詳細(xì)討論

    在android開發(fā)中我們有時(shí)候根據(jù)項(xiàng)目的需求多少會(huì)加載一些webview,加載webview,我們有時(shí)候會(huì)根據(jù)UI來自定義返回鍵,下面這篇文章主要給大家介紹了關(guān)于Android?WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • Android實(shí)現(xiàn)通知欄透明的方法

    Android實(shí)現(xiàn)通知欄透明的方法

    這個(gè)特性是andorid4.4支持的,最少要api19才可以使用,也就是說如果Android的機(jī)子是低于4.4,沉浸通知欄是沒有效果的。下面介紹一下使用的方法,非常得簡(jiǎn)單,對(duì)android通知欄透明相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android DataBinding的官方雙向綁定示例

    Android DataBinding的官方雙向綁定示例

    本篇文章主要介紹了Android DataBinding的官方雙向綁定示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 使用RecyclerView添加Header和Footer的方法

    使用RecyclerView添加Header和Footer的方法

    RecyclerView雖然作為L(zhǎng)istView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時(shí)會(huì)經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個(gè)方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹
    2016-03-03
  • Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

    Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

    這篇文章將借助開源庫?MLKit?實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功,該庫的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測(cè)等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下
    2023-08-08
  • android列表控件實(shí)現(xiàn)展開、收縮功能

    android列表控件實(shí)現(xiàn)展開、收縮功能

    這篇文章主要為大家詳細(xì)介紹了android支持展開/收縮功能的列表控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • android開發(fā)教程之系統(tǒng)資源的使用方法 android資源文件

    android開發(fā)教程之系統(tǒng)資源的使用方法 android資源文件

    這篇文章主要介紹了android中的系統(tǒng)資源的使用方法,包括顏色資源 、字符串資源、尺寸資源、XML資源文件,需要的朋友可以參考下
    2014-02-02
  • Android事件分發(fā)機(jī)制(下) View的事件處理

    Android事件分發(fā)機(jī)制(下) View的事件處理

    這篇文章主要介紹了Android事件分發(fā)機(jī)制下篇, View的事件處理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評(píng)論