亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android IntentService詳解及使用實例

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

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會。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

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

    Android控件系列之Shape使用方法

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

    100 行代碼實現(xiàn)Flutter自定義TabBar的示例代碼

    這篇文章主要介紹了100 行代碼實現(xiàn)Flutter自定義TabBar的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Android?WebView的使用與后退鍵處理詳細討論

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

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

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

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

    Android DataBinding的官方雙向綁定示例

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

    使用RecyclerView添加Header和Footer的方法

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

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

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

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

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

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

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

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

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

最新評論