Bound Service
可以实现多个组件绑定一个service。
适应binder的使用范围是1、同一个应用 2、同一个进程
下面我们通过一个例子来说明如何使用BounderService
先写一个布局 布局当中有两个按钮 一个是绑定一个是解除绑定
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/catalog_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/catalog_activity_top_bg_white"
android:clickable="true"
android:orientation="vertical">
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="testBindService"
android:text="bindService"
/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="testUnBindService"
android:text="unBindService"
/>
</LinearLayout>
我们现在书写service
public class TestService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.e("TestService", "onBind");
return new YuanxzhBinder();
}
@Override
public void onCreate() {
Log.e("TestService", "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TestService", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("TestService", "onDestroy");
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.e("TestService", "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public void onRebind(Intent intent) {
Log.e("TestService", "onRebind");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("TestService", "onUnbind");
return super.onUnbind(intent);
}
public class YuanxzhBinder extends Binder{
public TestService getTestService(){
return TestService.this;
}
}
public void printYuanxhTest(){
Log.e("TestService", "printYuanxhTest 1111111");
}
}
BounderService的关键回调是onBind,和StartService不同的地方是
StartService走onStartCommand
BindService走onBind,通过onBind返回一个实现Binder类的对象。
我们现在来写客户端
public class MainActivity extends AppCompatActivity {
ServiceConnection serviceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e("TestService", "onServiceConnected");
TestService.YuanxzhBinder binder = (TestService.YuanxzhBinder)iBinder;
TestService testService = (TestService)(binder.getTestService());
testService.printYuanxhTest();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e("TestService", "onServiceDisconnected");
}
};
}
public void testBindService(View view){
Log.e("TestService", "testBindService");
Intent intent = new Intent();
intent.setClass(this, TestService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
public void testUnBindService(View view){
Log.e("TestService", "testUnBindService");
unbindService(serviceConnection);
}
}
客户端调用bindService,实现和service的绑定。通过ServiceConnection的回调onServiceConnected来获取Service的实例
通过实例来调用service中公开的方法。
testService.printYuanxhTest();
我们来看一下运行过程
点击绑定后我们看一下执行过程
点击取消绑定或者退出软件可以看到
最后我们再来看一下service的生命周期图加深一下理解