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

說說在Android如何使用服務(wù)(Service)的方法

 更新時(shí)間:2018年06月25日 09:58:28   作者:deniro_li  
這篇文章主要介紹了說說在Android如何使用服務(wù)(Service)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Android 服務(wù)(Service)適合執(zhí)行那些不需要和用戶交互而且還要求長(zhǎng)期運(yùn)行的任務(wù)。

服務(wù)的運(yùn)行不依賴于任何用戶界面,即使 APP 被切換到后臺(tái),或者打開了另外一個(gè) APP,服務(wù)仍然能夠保持正常運(yùn)行。

但是當(dāng)某個(gè) APP 進(jìn)程被殺掉時(shí),那么這個(gè) APP 所創(chuàng)建的所有服務(wù)也就停止咯。

另外,服務(wù)本身并不會(huì)自動(dòng)開啟線程,服務(wù)代碼默認(rèn)是運(yùn)行在主線程中的。所以如果需要執(zhí)行的業(yè)務(wù)邏輯耗時(shí)長(zhǎng),那么為了防止主線程被阻塞,我們必須在服務(wù)內(nèi)部創(chuàng)建子線程來執(zhí)行這些業(yè)務(wù)邏輯。

1 定義服務(wù)

在 Android Studio 中可以通過 File→New→Service→Service 來創(chuàng)建服務(wù):

在彈出的對(duì)話框中配置服務(wù):

在此配置服務(wù)名。下面兩個(gè)配置項(xiàng)說明如下:

* Exported:是否允許除了當(dāng)前程序之外的其他程序訪問這個(gè)服務(wù)。(默認(rèn)勾選)

* Enabled:是否啟用這個(gè)服務(wù) 。 (默認(rèn)勾選)

public class FirstService extends Service {

 private static final String TAG = "FirstService";

 @Override
 public void onCreate() {
  super.onCreate();
  Log.d(TAG, "onCreate");
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.d(TAG, "onStartCommand");
  return super.onStartCommand(intent, flags, startId);
 }

 @Override
 public void onDestroy() {
  super.onDestroy();
  Log.d(TAG, "onDestroy");
 }

 public FirstService() {
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO: Return the communication channel to the service.
  throw new UnsupportedOperationException("Not yet implemented");
 }
}

創(chuàng)建好后的服務(wù)繼承自 Service,并且需要實(shí)現(xiàn) onBind() 方法。我們?cè)诖诉€重寫了以下幾個(gè)方法:

方法 說明
void onCreate() 服務(wù)創(chuàng)建時(shí)調(diào)用該方法。
onStartCommand(Intent intent, int flags, int startId) 每次服務(wù)啟動(dòng)時(shí)調(diào)用該方法。
void onDestroy() 服務(wù)銷毀時(shí)調(diào)用該方法。

我們還在這些方法中加入了日志,便于觀察運(yùn)行結(jié)果。

此外,在此類的任何位置調(diào)用 stopSelf() 方法,服務(wù)就會(huì)自行停止。

定義好服務(wù)后,需要在 AndroidManifest.xml 中注冊(cè)服務(wù)(如果用的是 Android Studio,那么這一步它已經(jīng)幫我們做啦):

<service
 android:name=".FirstService"
 android:enabled="true"
 android:exported="true"></service>

2 啟動(dòng)或停止服務(wù)

借助 Intent,我們就可以控制服務(wù)的啟動(dòng)與停止啦O(∩_∩)O哈哈~

final Context context = this;
findViewById(R.id.start_service).setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  //啟動(dòng)服務(wù)
  startService(new Intent(context, FirstService.class));
 }
});
findViewById(R.id.stop_service).setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  //停止服務(wù)
  stopService(new Intent(context, FirstService.class));
 }
});

這里的 startService()stopService() 方法都是定義在 Context 類中,所以在活動(dòng)類中可以直接調(diào)用。

執(zhí)行結(jié)果:

D/FirstService: onCreate   D/FirstService: onStartCommand D/FirstService: onDestroy

**注意:**onCreate() 在第一次創(chuàng)建服務(wù)時(shí)被調(diào)用,而 onStartCommand() 會(huì)在每次啟動(dòng)服務(wù)時(shí)被調(diào)用。

服務(wù)啟動(dòng)后,可以在 Android 的 Settings → Developer opinions → Running services 中發(fā)現(xiàn)它:

點(diǎn)擊 APP 后,可以看到服務(wù)詳情:

如果找不到 Developer opinions 選項(xiàng),請(qǐng)先在 Android 的 Settings → About emulated device → 多次點(diǎn)擊 Build number 就可以開啟開發(fā)者模式啦:

3 活動(dòng)控制服務(wù)

活動(dòng)是通過實(shí)現(xiàn) ServiceConnection 接口來與服務(wù)建立連接的,它包含以下兩個(gè)方法:

方法 說明
onServiceConnected(ComponentName name, IBinder service) 服務(wù)綁定后調(diào)用該方法。
onServiceDisconnected(ComponentName name) 服務(wù)解綁后調(diào)用該方法。

而服務(wù)是通過實(shí)現(xiàn) IBinder onBind(Intent intent) 方法來轉(zhuǎn)換為 onServiceConnected() 方法所需要的 IBinder 型的 service 參數(shù)的。

首先,我們修改服務(wù)類:

public class FirstService extends Service {
 class CustomBinder extends Binder {

  public void init(){
   Log.d(TAG, "init CustomBinder");
  }

 }

 @Override
 public IBinder onBind(Intent intent) {
  return new CustomBinder();
 }
}

這里,我們定義了一個(gè)內(nèi)部類 CustomBinder,里面實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的初始化方法。然后在 onBind() 方法中返回它的實(shí)例。

接著,我們修改活動(dòng)類,創(chuàng)建 ServiceConnection 實(shí)例:

private ServiceConnection connection = new ServiceConnection() {
 @Override
 public void onServiceConnected(ComponentName name, IBinder service) {
  Log.d(TAG, "onServiceConnected");
  FirstService.CustomBinder customBinder = (FirstService.CustomBinder) service;
  customBinder.init();
 }

 @Override
 public void onServiceDisconnected(ComponentName name) {
  Log.d(TAG, "onServiceDisconnected");
 }
};

現(xiàn)在,就可以通過 ServiceConnection 實(shí)例來綁定或者解綁服務(wù)啦。

綁定服務(wù):

復(fù)制代碼 代碼如下:
bindService(new Intent(context, FirstService.class), connection, BIND_AUTO_CREATE);

bindService 接收三個(gè)參數(shù):

參數(shù) 類型 說明
service Intent 這個(gè) Intent 將綁定當(dāng)前的活動(dòng)類與服務(wù)類。
conn ServiceConnection ServiceConnection 對(duì)象。
flags int 綁定服務(wù)的方式。這里的 BIND_AUTO_CREATE 表示綁定后自動(dòng)創(chuàng)建服務(wù)。

【綁定服務(wù)】輸出結(jié)果:

D/FirstService: onCreate D/MainActivity: onServiceConnected D/FirstService: init CustomBinder

解綁服務(wù):

unbindService(connection);

只要傳入 ServiceConnection 對(duì)象即可解綁,是不是很簡(jiǎn)單呀 O(∩_∩)O哈哈~

【解綁服務(wù)】輸出結(jié)果:

D/FirstService: onDestroy

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Shape屬性創(chuàng)建環(huán)形進(jìn)度條

    Android Shape屬性創(chuàng)建環(huán)形進(jìn)度條

    這篇文章主要介紹了Android Shape屬性創(chuàng)建環(huán)形進(jìn)度條的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 避免 Android中Context引起的內(nèi)存泄露

    避免 Android中Context引起的內(nèi)存泄露

    本文主要介紹Android中Context引起的內(nèi)存泄露的問題,這里對(duì)Context的知識(shí)做了詳細(xì)講解,說明如何避免內(nèi)存泄漏的問題,有興趣的小伙伴可以參考下
    2016-08-08
  • Android實(shí)現(xiàn)APP在線下載更新

    Android實(shí)現(xiàn)APP在線下載更新

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)APP在線下載更新的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多

    android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多

    這篇文章主要介紹了android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android中實(shí)現(xiàn)毛玻璃效果的3種方法

    Android中實(shí)現(xiàn)毛玻璃效果的3種方法

    這篇文章主要介紹了Android中實(shí)現(xiàn)毛玻璃效果的3種方法,本文講解了使用系統(tǒng)提供的方法、自定義的方法、C語言實(shí)現(xiàn)方法等3種方法,需要的朋友可以參考下
    2015-04-04
  • 淺談Android中Service的注冊(cè)方式及使用

    淺談Android中Service的注冊(cè)方式及使用

    下面小編就為大家分享一篇淺談Android中Service的注冊(cè)方式及使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Android指紋識(shí)別功能深入淺出分析到實(shí)戰(zhàn)(6.0以下系統(tǒng)解決方案)

    Android指紋識(shí)別功能深入淺出分析到實(shí)戰(zhàn)(6.0以下系統(tǒng)解決方案)

    指紋識(shí)別在現(xiàn)實(shí)應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識(shí)別功能,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • Android入門教程之組件Activity的生命周期詳解

    Android入門教程之組件Activity的生命周期詳解

    Activity作為四大組件之一,出現(xiàn)的頻率相當(dāng)高,基本上我們?cè)赼ndroid的各個(gè)地方都能看見它的蹤影,因此深入了解Activity,對(duì)于開發(fā)高質(zhì)量應(yīng)用程序是很有幫助的。今天我們就來詳細(xì)地聊聊Activity的生命周期,以便我們?cè)谝院蟮拈_發(fā)中能如魚得水
    2021-10-10
  • android studio 的下拉菜單Spinner使用詳解

    android studio 的下拉菜單Spinner使用詳解

    這篇文章主要介紹了android studio 的下拉菜單Spinner使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Android單選多選按鈕的使用方法

    Android單選多選按鈕的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android單選多選按鈕的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論