定義
Service是Android四大組件之一逾苫,也是可執(zhí)行的程序铺纽,有自己的生命周期。創(chuàng)建哟忍、配置Service和創(chuàng)建狡门、配置Activity的過程相似。和Activity一樣锅很,都是從Context派生出來的其馏。
分類
按啟動方式分
采用start的方式開啟服務(wù)
- 定義一個類繼承Service
- 在Manifest.xml文件中配置該Service
- 使用Context的startService(Intent)方法啟動該Service
- 不再使用時,調(diào)用stopService(Intent)方法停止該服務(wù)
生命周期
onCreate()--->onStartCommand()(onStart()方法已過時) ---> onDestory()
說明
??如果服務(wù)已經(jīng)開啟爆安,不會重復(fù)的執(zhí)行onCreate()叛复,而是會調(diào)用onStart()和onStartCommand()。服務(wù)停止的時候調(diào)用onDestory()扔仓。服務(wù)只會被停止一次褐奥。
特點
一旦服務(wù)開啟跟調(diào)用者(開啟者)就沒有任何關(guān)系了。開啟者退出了翘簇,開啟者掛了撬码,服務(wù)還在后臺長期的運(yùn)行。開啟者不能調(diào)用服務(wù)里面的方法版保。
示例
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public class LocalBinder extends Binder {
public LocalService getService() {
return LocalService.this;
}
}
/**
* method for clients
*/
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
// 調(diào)用
Intent intent = new Intent(this, LocalService.class);
startService(intent);
// 停止
stopService(intent)呜笑;
采用bind的方式開啟服務(wù)
步驟
1.定義一個類繼承Service
2.在Manifest.xml文件中配置該Service
3.使用Context的bindService(Intent, ServiceConnection, int)方法啟動該Service
4.不再使用時,調(diào)用unbindService(ServiceConnection)方法停止該服務(wù)
生命周期
onCreate() --->onBind()--->onunbind()--->onDestory()
注意
??綁定服務(wù)不會調(diào)用onstart()或者onstartcommand()方法
特點
??bind的方式開啟服務(wù)彻犁,綁定服務(wù)叫胁,調(diào)用者掛了,服務(wù)也會跟著掛掉汞幢。綁定者可以調(diào)用服務(wù)里面的方法驼鹅。
示例
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
/**
* method for clients
*/
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
// 綁定
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
// 獲取服務(wù)
private LocalService mService;
private boolean mBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
LocalService.LocalBinder binder = (LocalService.LocalBinder) iBinder;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBound = false;
}
};
// 調(diào)用服務(wù)的方法
if (mBound) {
int number = mService.getRandomNumber();
// do somtheing
}
// 調(diào)用者關(guān)閉時解綁服務(wù)
if (mBound) {
unbindService(mConnection);
mBound = false;
}