知识点:
Service中的onBind方法,返回IBinder接口,如果直接实现,方法数太多,因此返回自定义MyBinder继承Binder类
@Override
public IBinder onBind(Intent intent) {
return new IBinder() {
@Override
public String getInterfaceDescriptor() throws RemoteException {
return null;
}
@Override
public boolean pingBinder() {
return false;
}
@Override
public boolean isBinderAlive() {
return false;
}
合理的写法
@Override
public IBinder onBind(Intent intent) {
Log.i("zhang","服务 onBind");
return new MyBinder();
}
public class MyBinder extends Binder {
//自定义方法
//比如 我要获取这个service
public MyService getService() {
return MyService.this;
}
}
绑定服务会随着Activity结束而结束,启动服务则不受activity影响
MainActivity
private ServiceConnection mConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("zhang","服务 mConnection onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("zhang","服务 mConnection disConnected");
}
};
@butterknife.OnClick({R.id.start, R.id.stop, R.id.bind, R.id.unbind})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.start:
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
break;
case R.id.stop:
stopService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.bind:
Intent bindintent = new Intent(MainActivity.this, MyService.class);
bindService(bindintent,mConnection,BIND_AUTO_CREATE);
break;
case R.id.unbind:
unbindService(mConnection);
break;
}
}
MyService
@Override
public IBinder onBind(Intent intent) {
Log.i("zhang","服务 onBind");
return new MyBinder();
}
@Override
public void onCreate() {
super.onCreate();
Log.i("zhang","服务 onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("zhang","服务 onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("zhang","服务 onDestroy");
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("zhang","服务 onUnbind");
return super.onUnbind(intent);
}
public class MyBinder extends Binder {
//自定义方法
//比如 我要获取这个service
public MyService getService() {
return MyService.this;
}
}
AndroidManifest
运行
1. start -> stop
说明启动service,一定会调用onStartCommand方法
2. start -> start
重复启动service,只会调用一次onCreate方法,onStartCommand可多次调用
3. bind -> unbind
bind也能创建service,执行onCreate方法
4. bind -> start
bind之后start service,不会重新onCreate,只会执行onStartCommond
5. bind -> start -> stop
6. bind -> start -> unbind
bind创建的Service,start之后,unbind只能解绑,不能销毁
7.bind -> start -> unbind ->stop
同上5,6,7对比,说明bind创建的Service,start之后,如果要销毁service,必须先unbind,然后stop,才能销毁
8. bind -> bind
重复bind并未执行任何方法
9. bind -> start ->unbind ->bind
若要执行ServiceConnection的方法,必须先unbind,然后bind调用
即可获得值service中的值,达到实时监控service中的值方法
10. bind -> 点击返回键
activity销毁之后,bind的service也unbind,销毁
11. bind -> start ->点击返回键
中间加了start步骤,即使activity销毁之后,service也不会被销毁,只会解绑
案例:
耗时操作放在onStartCommond中执行
实时获取线程中的值,需要bind
bind之后,start执行,然后重复unbind bind操作,就可以通过serviceConn中的方法获取值
MainActivity
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.MyBinder binder = (MyService.MyBinder) service;
int count = binder.getCount();
Log.i("zhang", " mConnection onServiceConnected count " + count);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("zhang", "服务 mConnection disConnected");
}
};
MyService
public int count = 0;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.i("zhang","onBind");
return new MyBinder();
}
@Override
public void onCreate() {
Log.i("zhang","onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("zhang","onStartCommand");
new Thread(new Runnable() {
@Override
public void run() {
for (count = 0; count < 100; count++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("zhang","onUnbind");
return super.onUnbind(intent);
}
public class MyBinder extends Binder {
public int getCount(){
return count;
}
}
结果:
start -> unbind -> bind -> unbind -> bind......