Bound Service 1

本文详细介绍了Android中BoundService的使用方法,包括如何创建一个绑定服务、客户端如何与服务进行交互及解绑过程。通过示例代码展示了关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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不同的地方是

StartServiceonStartCommand

BindServiceonBind,通过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的生命周期图加深一下理解

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值