一、Service詳解
Service的運行不依賴于任何用戶界面。當某個應用程序進程被殺掉時,所有依賴于該進程的服務也會停止運行。Service默認并不會運行在子線程中,也不運行在一個獨立的進程中,它同樣執行在主線程中。
Service生命周期.png
(1)生命周期狀態
- onCreate:首次創建服務時調用。服務已在運行,不會調用此方法,該方法只調用一次。
- onDestroy:當服務不再使用且將被銷毀時調用。
- startService:啟動服務
- onStartCommand:組件通過調用startService()請求啟動服務時調用。
- stopService:關閉服務
- bindService:綁定服務
- onBind:組件通過調用bindService()與服務綁定時調用。
- unbindService:解綁服務
- onUnbind:組件通過調用unbindService()與服務解綁時調用。
- onRebind:舊的組件與服務解綁后,新的組件與服務綁定,onUnbind()返回true時調用。
(2)兩種啟動Service服務方式
- 啟動Service服務
單次:startService() onCreate() onStartCommand()
多次:startService() onCreate() onStartCommand() onStartCommand() - 停止Service服務
stopService() onDestroy()
無論調用多少次startService()方法,只需調用一次stopService()或stopSelf()方法,服務就會停止了。 - 綁定Service服務
bindService() onCreate() onBind() - 解綁Service服務
unbindService() onUnbind() onDestroy() - 啟動綁定Service服務
startService() onCreate() onStartCommand() bindService() onBind() - 解綁停止Service服務
unbindService() onUnbind() stopService() onDestroy() - 這兩種啟動方法并不沖突,當使用startService()啟動Service之后,還可再使用bindService()綁定,只不過需要同時調用 stopService()和 unbindService()方法才能讓服務銷毀掉。
(3)Service使用
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
- 在配置文件中進行注冊。
<service android:name=".MyService" />
- 在活動中利用Intent可實現Service的啟動。
Intent intent = new Intent(this, MyService.class);
startService(intent);
(4)其他Service
- 前臺Service:在MyService的onCreate創建Notification實例,調用startForeground()方法
- 系統Service:通過getSyetemService()方法并傳入一個Name就可以得到相應的服務對象。
ALARM_SERVICE:鬧鐘服務
POWER_SERVICE:電源服務
KEYAUARD_SERVICE:鍵盤鎖服務
NOTIFICATION_SERVICE:狀態欄服務
WINDOW_SERVICE:管理打開的窗口程序
ACTIVITY_SERVICE:管理應用程序的系統狀態 - IntentService:異步的、會自動停止的服務
(5)Service與Activity的通信
public class MyService extends Service {
private MyBinder myBinder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
System.out.println("執行了onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("執行了onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
System.out.println("執行了onDestroy");
super.onDestroy();
}
//返回定義好的MyBinder類
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("執行了onBind");
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("執行了onUnbind");
return super.onUnbind(intent);
}
//自定義一個類MyBinder并繼承Binder,在它的內部提供關聯了activity方法
class MyBinder extends Binder {
void serviceActivity(){
System.out.println("Service關聯Activity");
}
}
}
Activity代碼
private MyService.MyBinder myBinder;
//Activity中實例化一個ServiceConnection類
private ServiceConnection serviceConnection = new ServiceConnection() {
//服務成功綁定時調用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.serviceActivity();
}
//解除綁定時調用
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
//綁定Service
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
//解綁Service
unbindService(serviceConnection);