转载请注明出处:http://blog.csdn.net/wei_chong_chong/article/details/51614537
案例是本地绑定服务
package com.example.service1;
import com.example.service1.MyService.DownloadBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
downloadBinder = (DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgerss();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int myid = v.getId();
if(myid == R.id.start_service){
Intent startintent = new Intent(this, MyService.class);
startService(startintent);
}
else if(myid == R.id.stop_service){
Intent stopservice = new Intent(this, MyService.class);
stopService(stopservice);
}else if(myid == R.id.bind_service){
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
}else if(myid == R.id.unbind_service){
unbindService(connection); // 解绑服务
}
}
}
情景一:正常启动关闭服务
点击startService启动服务:onCreate->onStartCommand
点击stopService:onDestory()
情景二:(启动一个已经启动的服务)
点击startService启动服务:onCreate->onStartCommand
再次点击startService启动服务:onStartCommand(这时不再调用onCreate,)
点击绑定服务:onCreate->onBind
点击解除绑定服务:onUnbind->onDestory
情景四:启动和绑定混合(1)
点击启动服务:onCreate->onStartCommand
点击绑定服务:onBind
点击解除绑定服务:onUnbind(这中情景不会自动调用onDestory方法了)
点击关闭服务:onDestory(这种情况只有调用stopService方法才能关闭服务)
情景五:启动和绑定混合(2)
点击绑定服务:onCreate->onBind
点击启动服务:onStartCommand
点击解绑定服务:onUnbind
点击关闭服务:onDestory 只有调用stopService可以看到这种情况与情景四类似
情景六:启动和绑定混合(3)
点击启动服务:onCreate->onStartCommand
点击绑定服务:onBind
点击关闭服务:无效果
点击解除绑定服务:onUnbind ->onDestory
情景七:启动和绑定混合(4)
点击绑定服务:onCreate->onBind
点击启动服务:onStartCommand
点击关闭服务:这几个方法都不调用,无效果
点击解绑定服务:onUnbind->onDestory
补充一种情景:启动和绑定混合(5)
点击绑定服务:onCreate->onBind
点击启动服务:onStartCommand
点击关闭服务:这几个方法都不调用,无效果
点击启动服务:onStartCommand
点击解绑定服务:onUnbind
点击关闭服务:onDestory
由情景四和情景五可以总结一下:
绑定服务和启动服务(先后顺序可调)后,必须同时调用解绑定和停止服务方法才能彻底onDestory服务
由情景 六,七可得:同时启动和绑定服务(先后顺序可调)后,调用stopService在onUnbind之前,stopService会将自己要做的工作给onUnbind(所以这个时候onUnbind会调用onDestory)
由补充的情景知:stopService必须和onUnbind 挨着同时调用才有onDestory的效果,才有情景七的效果
情景八:未启动服务直接关闭服务
关闭服务:这几个方法都不调用,但不报错(如果服务未启动就调用stopService,这几个方法都不会执行,相当于没效果
情景九:未启动服务且没有绑定服务,直接点击解绑定服务
直接点击解绑定服务:这几个方法都不执行,且程序报错
情景十:启动服务后解绑定
启动服务:onCreate->onStartCommand
解绑定服务:效果同情景九
由情景三,九,十可得到:必须绑定服务才能调用解绑定服务方法
情景十一 :
绑定服务:onCreate->onBind
停止服务:这几个方法都没调用,相当于没效果
解绑定:onUnBind->onDestory
由情景一,八可以得出:只有启动服务之后,调用停止服务才有效果
补充总结:stopService只对startService有效果,unbindService只对bindService起作用。如果在调用unbindService之前调用了startService,unBindService方法不再自动调用onDestory方法,否则会自动调用onDestory