android studio BindService

一.输入:

1.全部代码:

主界面代码:

public class BindServiceActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "BindServiceActivity";
    private Button mBtBindService;
    private Button mBtnUnbindService;
    MyBindService myBindService;
    boolean isBind=false;
    ServiceConnection coon = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        //绑定服务的时候偶触发
            Log.d(TAG, "onServiceConnected: ");
            MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;
            myBindService = myBinder.getService();
            isBind=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            //解绑的时候触发
        }
    };
    private Button mBtGetData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_service);
        initView();
    }

    private void initView() {
        mBtBindService = (Button) findViewById(R.id.bt_bind_service);
        mBtnUnbindService = (Button) findViewById(R.id.btn_unbind_service);

        mBtBindService.setOnClickListener(this);
        mBtnUnbindService.setOnClickListener(this);
        mBtGetData = (Button) findViewById(R.id.bt_get_data);
        mBtGetData.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_bind_service:
                Intent intent = new Intent(BindServiceActivity.this, MyBindService.class);
                bindService(intent, coon, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbind_service:
                if (isBind){
                    unbindService(coon);
                    isBind=false;
                }

                break;
            case R.id.bt_get_data:
                Toast.makeText(myBindService, "随机数为:"+myBindService.getValues(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

服务代码:

public class MyBindService extends Service {
    private static final String TAG = "MyBindService";
    private IBinder iBinder;
    private Random mRandom;

    public MyBindService() {
    }
    public class MyBinder extends Binder{
        MyBindService getService(){
            return MyBindService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return iBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
        iBinder = new MyBinder();
        mRandom = new Random();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.d(TAG, "onRebind: ");
    }
    public int getValues(){
        return mRandom.nextInt(100);
    }
}

声明:

service
            android:name=".MyBindService"
            android:enabled="true"
            android:exported="true"></service>

xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BindServiceActivity">


    <Button
        android:id="@+id/bt_bind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绑定服务"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_unbind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="解绑服务"
        app:layout_constraintEnd_toEndOf="@+id/bt_bind_service"
        app:layout_constraintStart_toStartOf="@+id/bt_bind_service"
        app:layout_constraintTop_toBottomOf="@+id/bt_bind_service" />

    <Button
        android:id="@+id/bt_get_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="get data"
        app:layout_constraintEnd_toEndOf="@+id/btn_unbind_service"
        app:layout_constraintStart_toStartOf="@+id/btn_unbind_service"
        app:layout_constraintTop_toBottomOf="@+id/btn_unbind_service" />

</androidx.constraintlayout.widget.ConstraintLayout>

2.步骤:

1.创建绑定服务
  1. 在这里插入图片描述
  2. 在这里插入图片描述
    实现这些方法:onCreate,onBind,onUnbind,onDestroy
    这个方法中要返回一个IBinder
    在这里插入图片描述
    我们创建一个类继承自Binder(他实现接口是IBinder)
public class MyBinder extends Binder{
        MyBindService getService(){
            return MyBindService.this;//获取自己
        }
    }

初始化我们创建的类,并设置为成员变量

@Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
        iBinder = new MyBinder();
        mRandom = new Random();
    }
private IBinder iBinder;
2. 绑定服务
Intent intent = new Intent(BindServiceActivity.this, MyBindService.class);
                bindService(intent, coon, Context.BIND_AUTO_CREATE);//绑定服务(参数:intent,绑定监听,常量)

创建绑定监听coon

/**
     * 服务绑定连接监听
     */
    ServiceConnection coon = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {//绑定连接触发
            Log.d(TAG, "onServiceConnected: ");
            MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;
            myBindService = myBinder.getService();
            isBind=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {//绑定不连接触发
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };
3.取消服务:
if (isBind){//没绑定的话取消会报错
                    unbindService(coon);//取消服务
                    isBind=false;
                }
4.生命周期

在这里插入图片描述
流程图
在这里插入图片描述

二.输出:

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值