国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Service類與生命周期詳細介紹

2019-12-12 03:27:55
字體:
來源:轉載
供稿:網友

Android  Service類與生命周期

Service是Android四大組件與Activity最相似的組件,都代表可執行的程序,區別在于Service一直在后臺運行且沒有用戶界面。

1.Service的類圖和生命周期

先來看看Service的類圖:

這里寫圖片描述

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

這里寫圖片描述

2.開發Service

(1)開發Service需要兩步:

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

(2)創建Service

public class MyService extends Service {  // 必須實現,綁定該Service時被回調  @Override  public IBinder onBind(Intent intent) {    return null;  }  // Service被創建時回調  @Override  public void onCreate() {    super.onCreate();    // 定義相關業務邏輯    System.out.println("Service is Created");  }  // Service被啟動時回調  @Override  public int onStartCommand(Intent intent, int flags, int startId) {    // 定義相關業務邏輯    System.out.println("Service is Started");    return START_STICKY;  }  // Service被關閉之前回調  @Override  public void onDestroy() {    super.onDestroy();    System.out.println("Service is Destroyed");  }}

(3)配置Service

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

接下來就可以運行Service了。

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

// 創建啟動Service的Intentfinal Intent intent = new Intent();// 為Intent設置Action屬性intent.setAction("com.gc.service.MY_SERVICE");...// 啟動指定SerivcestartService(intent);...// 停止指定SerivcestopService(intent);

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

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

如果Service和訪問者之間需要進行方法調用或數據交換,則應該使用bindService()和unbindService()方法啟動、停止Service。

bindService(Intent intent, ServiceConnection conn, int flags),三個參數如下:intent:指定要啟動的Serviceconn:用于監聽訪問者與Service之間的連接情況,當訪問者與Service之間連接成功時將回調該ServiceConnection對象的onServiceConnected(ComponentName name, IBinder service)方法;反之回調該ServiceConnection對象的onServiceDisconnected(ComponentName name)方法(主動調用unbindService方法斷開連接時則不回調)flags:指定綁定時是否創建Service,0:不自動創建;BIND_AUTO_CREATE:自動創建注意:ServiceConnection對象的onServiceConnected方法中有一個IBinder對象,該對象即可實現與綁定Service之間的通信。在綁定本地Service的情況下,onBind(Intent intent)方法所返回的IBinder對象將會傳給ServiceConnection對象里onServiceConnected(ComponentName name, IBinder service)方法的service參數,這樣訪問者就可以通過該IBinder對象與Service進行通信。

實際開發通常會采用繼承Binder(IBinder的實現類)的方式實現自己的IBinder對象。

public class MyService extends Service {  private int count;  // 定義onBinder方法所返回的對象  private MyBinder binder = new MyBinder();  // 通過繼承Binder來實現IBinder類  public class MyBinder extends Binder {    public int getCount() {      return count; // 獲取Service的運行狀態    }  }  // 必須實現,綁定該Service時被回調  @Override  public IBinder onBind(Intent intent) {    System.out.println("Service is Binded");    return binder; // 返回IBinder對象  }  // Service被創建時回調  @Override  public void onCreate() {    super.onCreate();    System.out.println("Service is Created");    count = 100;  }  // Service被斷開連接時回調  @Override  public boolean onUnbind(Intent intent) {    System.out.println("Service is Unbinded");    return true;  }  // Service被關閉之前回調  @Override  public void onDestroy() {    super.onDestroy();    System.out.println("Service is Destroyed");  }}

接下來定義一個Activity來綁定該Service,并在該Activity中通過MyBinder對象訪問Service的內部狀態。

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

public class MyServiceTest extends Activity {  // Service的IBinder對象  MyService.MyBinder binder;  // 定義一個ServiceConnection對象  private ServiceConnection conn = new ServiceConnection() {    // 當該Activity與Service連接成功時回調    @Override    public void onServiceConnected(ComponentName name, IBinder service) {      // 獲取Service的onBind方法所返回的MyBinder對象      binder = (MyService.MyBinder) service;    }    // 當該Activity與Service斷開連接時回調    @Override    public void onServiceDisconnected(ComponentName name) {    }  };  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ...    // 創建啟動Service的Intent    final Intent intent = new Intent();    // 為Intent設置Action屬性    intent.setAction("com.gc.service.MY_SERVICE");    // 綁定指定Serivce    bindService(intent, conn, Service.BIND_AUTO_CREATE);    ...    binder.getCount(); // 獲取Serivce的count值    ...       // 解除綁定Serivce    unbindService(conn);  }}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平江县| 井冈山市| 大洼县| 洛宁县| 安乡县| 宁晋县| 克什克腾旗| 泸定县| 彰化市| 恭城| 堆龙德庆县| 六安市| 黎川县| 蓬莱市| 淅川县| 通化市| 寻甸| 上饶市| 霍城县| 宜城市| 格尔木市| 东平县| 大竹县| 永清县| 南通市| 万荣县| 新余市| 开江县| 黄冈市| 呼图壁县| 大连市| 芮城县| 盐池县| 始兴县| 临湘市| 太湖县| 钟山县| 安仁县| 玛纳斯县| 囊谦县| 德阳市|