当前位置: 萬仟网 > 移动技术>移动开发>Android > android studio开发中如何 BindService

android studio开发中如何 BindService

2020年10月12日  | 萬仟网移动技术  | 我要评论
代码:主界面代码:public class BindServiceActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "BindServiceActivity"; private Button mBtBindService; private Button mBtnUnbindService; MyBindService

一.输入:

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.生命周期

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

二.输出:

本文地址:https://blog.csdn.net/sunweihao2019/article/details/109002507

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
Copyright © 2017-2020  萬仟网 保留所有权利. 粤ICP备17035492号