Android Service類與生命周期詳細介紹
Android Service類與生命周期
Service是Android四大組件與Activity最相似的組件,都代表可執(zhí)行的程序,區(qū)別在于Service一直在后臺運行且沒有用戶界面。
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 {
// 必須實現(xiàn),綁定該Service時被回調(diào)
@Override
public IBinder onBind(Intent intent) {
return null;
}
// Service被創(chuàng)建時回調(diào)
@Override
public void onCreate() {
super.onCreate();
// 定義相關(guān)業(yè)務邏輯
System.out.println("Service is Created");
}
// Service被啟動時回調(diào)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 定義相關(guān)業(yè)務邏輯
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
...
<!-- 配置一個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(一般方式)
// 創(chuàng)建啟動Service的Intent
final Intent intent = new Intent();
// 為Intent設置Action屬性
intent.setAction("com.gc.service.MY_SERVICE");
...
// 啟動指定Serivce
startService(intent);
...
// 停止指定Serivce
stopService(intent);
當程序使用startService()、stopService()啟動、關(guān)閉Service時,Service與訪問者之間無法進行通信、數(shù)據(jù)交換,故下面介紹另一種方式啟動和停止Service。
(5)啟動和停止Service(綁定Service并與之通信)
如果Service和訪問者之間需要進行方法調(diào)用或數(shù)據(jù)交換,則應該使用bindService()和unbindService()方法啟動、停止Service。
bindService(Intent intent, ServiceConnection conn, int flags),三個參數(shù)如下: intent:指定要啟動的Service conn:用于監(jiān)聽訪問者與Service之間的連接情況,當訪問者與Service之間連接成功時將回調(diào)該ServiceConnection對象的onServiceConnected(ComponentName name, IBinder service)方法;反之回調(diào)該ServiceConnection對象的onServiceDisconnected(ComponentName name)方法(主動調(diào)用unbindService方法斷開連接時則不回調(diào)) flags:指定綁定時是否創(chuàng)建Service,0:不自動創(chuàng)建;BIND_AUTO_CREATE:自動創(chuàng)建 注意:ServiceConnection對象的onServiceConnected方法中有一個IBinder對象,該對象即可實現(xiàn)與綁定Service之間的通信。 在綁定本地Service的情況下,onBind(Intent intent)方法所返回的IBinder對象將會傳給ServiceConnection對象里onServiceConnected(ComponentName name, IBinder service)方法的service參數(shù),這樣訪問者就可以通過該IBinder對象與Service進行通信。
實際開發(fā)通常會采用繼承Binder(IBinder的實現(xiàn)類)的方式實現(xiàn)自己的IBinder對象。
public class MyService extends Service {
private int count;
// 定義onBinder方法所返回的對象
private MyBinder binder = new MyBinder();
// 通過繼承Binder來實現(xiàn)IBinder類
public class MyBinder extends Binder {
public int getCount() {
return count; // 獲取Service的運行狀態(tài)
}
}
// 必須實現(xiàn),綁定該Service時被回調(diào)
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
return binder; // 返回IBinder對象
}
// Service被創(chuàng)建時回調(diào)
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
count = 100;
}
// Service被斷開連接時回調(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");
}
}
接下來定義一個Activity來綁定該Service,并在該Activity中通過MyBinder對象訪問Service的內(nèi)部狀態(tài)。
在該Activity綁定該Service后,該Activity還可以通過MyBinder對象來獲取Service的運行狀態(tài)。對于Service的onBind(Intent intent)方法返回的IBinder對象來說,Service允許客戶端通過該IBinder對象來訪問Service內(nèi)部的數(shù)據(jù),這樣即可實現(xiàn)客戶端與Service之間的通信。
public class MyServiceTest extends Activity {
// Service的IBinder對象
MyService.MyBinder binder;
// 定義一個ServiceConnection對象
private ServiceConnection conn = new ServiceConnection() {
// 當該Activity與Service連接成功時回調(diào)
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 獲取Service的onBind方法所返回的MyBinder對象
binder = (MyService.MyBinder) service;
}
// 當該Activity與Service斷開連接時回調(diào)
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// 創(chuàng)建啟動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);
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 詳解Android中的Service
- Android IntentService詳解及使用實例
- Android 如何保證service在后臺不被kill
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實現(xiàn)微信自動向附近的人打招呼(AccessibilityService)
- Android AccessibilityService實現(xiàn)微信搶紅包插件
- Android Service中使用Toast無法正常顯示問題的解決方法
- Android基于service實現(xiàn)音樂的后臺播放功能示例
- Android Service的啟動過程分析
相關(guān)文章
Android 3D滑動菜單完全解析 Android實現(xiàn)推拉門式的立體特效
這篇文章主要為大家詳細介紹了Android 3D滑動菜單,Android實現(xiàn)推拉門式的立體特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
詳解Java編程中的反射在Android開發(fā)中的應用
這篇文章主要介紹了詳解Java編程中的反射在Android開發(fā)中的應用,主要來獲取安卓系統(tǒng)的屬性值,需要的朋友可以參考下2015-07-07

