通常有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。
1 通过startService
Service会经历 onCreate 到onStart,然后处于运行状态,stopService的时候调用onDestroy方法。
2 通过bindService
Service会运行onCreate,然后是调用onBind, 这个时候调用者和Service绑定在一起。调用者退出了,Srevice就会调用onUnbind->onDestroyed方法。
需要注意的是如果这几个方法交织在一起的话,会出现什么情况呢?
一个原则是Service的onCreate的方法只会被调用一次,就是你无论多少次的startService又bindService,Service只被创建一次。
如果先是bind了,那么start的时候就直接运行Service的onStart方法,如果先是start,那么bind的时候就直接运行onBind方法。
如果service运行期间调用了bindService,这时候再调用stopService的话,service是不会调用onDestroy方法的,service就stop不掉了,只能先UnbindService, 再StopService。
如果一个service通过startService 被start之后,多次调用startService 的话,service会多次调用onStart方法。多次调用stopService的话,service只会调用一次onDestroyed方法。
如果一个service通过bindService被start之后,多次调用bindService的话,service只会调用一次onBind方法。多次调用unbindService的话会出异常。
下面贴出一个service(在service中播放背景音乐)
public class PlayerService extends Service {
private static final String TAG = "MyService";
protected MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
Log.i("PlayerService", "onBind");
mediaPlayer.start();
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("PlayerService", "onUnbind");
if(mediaPlayer!=null&& mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
return true;
}
/** Called when the activity is first created. */
@Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.min6);
mediaPlayer.setLooping(false);
Log.i("PlayerService", "onCreate");
}
@Override
public void onStart(Intent intent, int startid) {
mediaPlayer.start();
Log.i("PlayerService", "onStart");
}
@Override
public void onDestroy() {
Log.i("PlayerService", "onDestroy");
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
Activity类来启动Service:
public class PlayerServiceActivity extends Activity {
protected ServiceConnection serviceConnection;
protected Button startButton;
protected Button stopButton;
protected Button bindButton;
protected Button unbindButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
startButton = (Button) findViewById(R.id.start);
stopButton = (Button) findViewById(R.id.stop);
bindButton = (Button) findViewById(R.id.bind);
unbindButton = (Button) findViewById(R.id.unbind);
unbindButton.setEnabled(false);
stopButton.setEnabled(false);
serviceConnection = new MyServiceConnection();
setListensers();
}
private class MyServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MyServiceConnection", "onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.i("MyServiceConnection", "onServiceConnected");
}
}
private void setListensers() {
startButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
//startButton.setEnabled(false);
startService(new Intent(PlayerServiceActivity.this, PlayerService.class));
stopButton.setEnabled(true);
}
});
stopButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
//stopButton.setEnabled(false);
Log.i("serviceConnection", "stop service");
stopService(new Intent(PlayerServiceActivity.this, PlayerService.class));
startButton.setEnabled(true);
}
});
bindButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
//bindButton.setEnabled(false);
bindService(new Intent(PlayerServiceActivity.this, PlayerService.class), serviceConnection,
BIND_AUTO_CREATE);
unbindButton.setEnabled(true);
}
});
unbindButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
//unbindButton.setEnabled(false);
unbindService(serviceConnection);
Log.i("serviceConnection", "unbind service");
bindButton.setEnabled(true);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}