Android(服务和活动的绑定)

Android

服务与活动的绑定

  • 简介:为了能让活动和服务的联系更加紧密一些,让服务更好的为活动提供一些功能上的服务,这边用一些方法把他们绑定,进行一些操作。
  • 流程简介
  1. 在服务类里创建一个继承自Binder类的类,在该类里可以自定义一些方法,这个方法可以涉及服务里的一些属性,也可以单纯的就是一个方法,并定义一个该类的对象。
  2. 重写服务的public IBinder onBind(Intent intent)方法,在这个方法里返回第一步里定义好得对象。
  3. 在活动中定义一个ServiceConnection对象,实现为一个该类型的匿名类,重写以下俩个方法
    public void onServiceConnected(ComponentName name, IBinder service)
    public void onServiceDisconnected(ComponentName name)
    在onServiceConnected中的参数,service就是系统调用绑定时会触发服务中的onBind类返回回来的对象,在活动中定义一个这种类型的引用,用来接收这个对象
  4. 在活动中定义好意图(Intent)对象,参数是this和这个服务类。然后使用bindService(intent,connection,BIND_AUTO_CREATE);方法来启动这个意图,其中第一个参数是意图对象,第二个参数是第三步定义的对象,第三个参数暂时还不是很懂,照抄就是了。
  5. 通过第四步启动后就绑定上了,会触发我们之前写的connection对象中的方法,然后取得服务里的Binder对象,然后通过调用这个对象的一些方法,就可以让服务为活动提供一些后台的服务(好别扭的话)。
  • 示例代码
    这边写一个活动调用服务里的加法方法实现俩数相加的效果的案例
    活动
public class MainActivity extends AppCompatActivity {
    private MyService.MyBinder myBinder;   //定义Binder引用
    private Button button;                 //取得页面的按钮
    private EditText textView1;            //取得第一个输入框
    private EditText textView2;            //取得第二个输入框
    private TextView textView3;            //取得结果文本
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        //重写方法来获取Binder对象
            myBinder = (MyService.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //取得各种UI对象
        button = findViewById(R.id.button);
        textView1 = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView5);
        //定义意图
        Intent intent = new Intent(this, MyService.class);
        //绑定服务
        bindService(intent,connection,BIND_AUTO_CREATE);
        //给按钮加上事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a = textView1.getText().toString();
                String b = textView2.getText().toString();
                int c = myBinder.Add(Integer.parseInt(a),Integer.parseInt(b));
                textView3.setText(Integer.toString(c));
            }
        });
    }
}

服务

public class MyService extends Service {
    private final MyBinder myBinder = new MyBinder();
    static class MyBinder extends Binder {
    //自定义继承Binder类
        public int Add(int a,int b)
        {
            return a+b;
        }
    }
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }
}

布局

<?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=".MainActivity">

    <EditText
        android:id="@+id/textView"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:autofillHints="12"
        android:hint="@string/_1"
        android:inputType="date"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/textView2"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginStart="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:autofillHints="12"
        android:hint="@string/_1"
        android:inputType="date"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="13dp"
        android:layout_height="25dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="40dp"
        android:text="+"
        android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="40dp"
        android:text="="
        android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="90dp"
        android:layout_height="35dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="30dp"
        android:text="2"
        android:textSize="30sp"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="计算和"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

效果图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值