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

Android  Service類與生命周期詳細(xì)介紹

 更新時(shí)間:2017年03月07日 09:14:51   投稿:lqh  
這篇文章主要介紹了Android Service類與生命周期詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下

Android  Service類與生命周期

Service是Android四大組件與Activity最相似的組件,都代表可執(zhí)行的程序,區(qū)別在于Service一直在后臺(tái)運(yùn)行且沒有用戶界面。

1.Service的類圖和生命周期

先來看看Service的類圖:

這里寫圖片描述

接下來看看Service的生命周期:

這里寫圖片描述

2.開發(fā)Service

(1)開發(fā)Service需要兩步:

第1步:定義子類,繼承Service
第2步:在AndroidManifest.xml文件中配置Service

(2)創(chuàng)建Service

public class MyService extends Service {
  // 必須實(shí)現(xiàn),綁定該Service時(shí)被回調(diào)
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  // Service被創(chuàng)建時(shí)回調(diào)
  @Override
  public void onCreate() {
    super.onCreate();
    // 定義相關(guān)業(yè)務(wù)邏輯
    System.out.println("Service is Created");
  }
  // Service被啟動(dòng)時(shí)回調(diào)
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    // 定義相關(guān)業(yè)務(wù)邏輯
    System.out.println("Service is Started");
    return START_STICKY;
  }
  // Service被關(guān)閉之前回調(diào)
  @Override
  public void onDestroy() {
    super.onDestroy();
    System.out.println("Service is Destroyed");
  }
}

(3)配置Service

<application
  ...
  <!-- 配置一個(gè)Service組件 -->
  <service android:name=".MyService">
    <intent-filter>
      <!-- 為該Service組件的intent-filter配置action -->
      <action android:name="com.gc.service.MY_SERVICE" />
    </intent-filter>
  </service>
</application>

接下來就可以運(yùn)行Service了。

(4)啟動(dòng)和停止Service(一般方式)

// 創(chuàng)建啟動(dòng)Service的Intent
final Intent intent = new Intent();
// 為Intent設(shè)置Action屬性
intent.setAction("com.gc.service.MY_SERVICE");
...
// 啟動(dòng)指定Serivce
startService(intent);
...
// 停止指定Serivce
stopService(intent);

當(dāng)程序使用startService()、stopService()啟動(dòng)、關(guān)閉Service時(shí),Service與訪問者之間無法進(jìn)行通信、數(shù)據(jù)交換,故下面介紹另一種方式啟動(dòng)和停止Service。

(5)啟動(dòng)和停止Service(綁定Service并與之通信)

如果Service和訪問者之間需要進(jìn)行方法調(diào)用或數(shù)據(jù)交換,則應(yīng)該使用bindService()和unbindService()方法啟動(dòng)、停止Service。

bindService(Intent intent, ServiceConnection conn, int flags),三個(gè)參數(shù)如下:

intent:指定要啟動(dòng)的Service

conn:用于監(jiān)聽訪問者與Service之間的連接情況,當(dāng)訪問者與Service之間連接成功時(shí)將回調(diào)該ServiceConnection對(duì)象的onServiceConnected(ComponentName name, IBinder service)方法;反之回調(diào)該ServiceConnection對(duì)象的onServiceDisconnected(ComponentName name)方法(主動(dòng)調(diào)用unbindService方法斷開連接時(shí)則不回調(diào))

flags:指定綁定時(shí)是否創(chuàng)建Service,0:不自動(dòng)創(chuàng)建;BIND_AUTO_CREATE:自動(dòng)創(chuàng)建

注意:ServiceConnection對(duì)象的onServiceConnected方法中有一個(gè)IBinder對(duì)象,該對(duì)象即可實(shí)現(xiàn)與綁定Service之間的通信。
在綁定本地Service的情況下,onBind(Intent intent)方法所返回的IBinder對(duì)象將會(huì)傳給ServiceConnection對(duì)象里onServiceConnected(ComponentName name, IBinder service)方法的service參數(shù),這樣訪問者就可以通過該IBinder對(duì)象與Service進(jìn)行通信。

實(shí)際開發(fā)通常會(huì)采用繼承Binder(IBinder的實(shí)現(xiàn)類)的方式實(shí)現(xiàn)自己的IBinder對(duì)象。

public class MyService extends Service {
  private int count;
  // 定義onBinder方法所返回的對(duì)象
  private MyBinder binder = new MyBinder();

  // 通過繼承Binder來實(shí)現(xiàn)IBinder類
  public class MyBinder extends Binder {
    public int getCount() {
      return count; // 獲取Service的運(yùn)行狀態(tài)
    }
  }
  // 必須實(shí)現(xiàn),綁定該Service時(shí)被回調(diào)
  @Override
  public IBinder onBind(Intent intent) {
    System.out.println("Service is Binded");
    return binder; // 返回IBinder對(duì)象
  }
  // Service被創(chuàng)建時(shí)回調(diào)
  @Override
  public void onCreate() {
    super.onCreate();
    System.out.println("Service is Created");
    count = 100;
  }
  // Service被斷開連接時(shí)回調(diào)
  @Override
  public boolean onUnbind(Intent intent) {
    System.out.println("Service is Unbinded");
    return true;
  }
  // Service被關(guān)閉之前回調(diào)
  @Override
  public void onDestroy() {
    super.onDestroy();
    System.out.println("Service is Destroyed");
  }
}

接下來定義一個(gè)Activity來綁定該Service,并在該Activity中通過MyBinder對(duì)象訪問Service的內(nèi)部狀態(tài)。

在該Activity綁定該Service后,該Activity還可以通過MyBinder對(duì)象來獲取Service的運(yùn)行狀態(tài)。對(duì)于Service的onBind(Intent intent)方法返回的IBinder對(duì)象來說,Service允許客戶端通過該IBinder對(duì)象來訪問Service內(nèi)部的數(shù)據(jù),這樣即可實(shí)現(xiàn)客戶端與Service之間的通信。

public class MyServiceTest extends Activity {
  // Service的IBinder對(duì)象
  MyService.MyBinder binder;

  // 定義一個(gè)ServiceConnection對(duì)象
  private ServiceConnection conn = new ServiceConnection() {
    // 當(dāng)該Activity與Service連接成功時(shí)回調(diào)
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      // 獲取Service的onBind方法所返回的MyBinder對(duì)象
      binder = (MyService.MyBinder) service;
    }
    // 當(dāng)該Activity與Service斷開連接時(shí)回調(diào)
    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    // 創(chuàng)建啟動(dòng)Service的Intent
    final Intent intent = new Intent();
    // 為Intent設(shè)置Action屬性
    intent.setAction("com.gc.service.MY_SERVICE");
    // 綁定指定Serivce
    bindService(intent, conn, Service.BIND_AUTO_CREATE);
    ...
    binder.getCount(); // 獲取Serivce的count值
    ...   
    // 解除綁定Serivce
    unbindService(conn);
  }
}


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

相關(guān)文章

最新評(píng)論