一.输入:
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() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//绑定服务的时候偶触发Log.d(TAG, "onServiceConnected: ");MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;myBindService = myBinder.getService();isBind=true;}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d(TAG, "onServiceDisconnected: ");//解绑的时候触发}};private Button mBtGetData;@Overrideprotected 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);}@Overridepublic 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;}}@Overridepublic IBinder onBind(Intent intent) {Log.d(TAG, "onBind: ");// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");return iBinder;}@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "onCreate: ");iBinder = new MyBinder();mRandom = new Random();}@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy: ");}@Overridepublic boolean onUnbind(Intent intent) {Log.d(TAG, "onUnbind: ");return super.onUnbind(intent);}@Overridepublic void onRebind(Intent intent) {super.onRebind(intent);Log.d(TAG, "onRebind: ");}public int getValues(){return mRandom.nextInt(100);}
}
声明:
serviceandroid: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"><Buttonandroid: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" /><Buttonandroid: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" /><Buttonandroid: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.创建绑定服务
实现这些方法:onCreate,onBind,onUnbind,onDestroy
这个方法中要返回一个IBinder
我们创建一个类继承自Binder(他实现接口是IBinder)
public class MyBinder extends Binder{MyBindService getService(){return MyBindService.this;//获取自己}}
初始化我们创建的类,并设置为成员变量
@Overridepublic 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() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//绑定连接触发Log.d(TAG, "onServiceConnected: ");MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;myBindService = myBinder.getService();isBind=true;}@Overridepublic void onServiceDisconnected(ComponentName name) {//绑定不连接触发Log.d(TAG, "onServiceDisconnected: ");}};
3.取消服务:
if (isBind){//没绑定的话取消会报错unbindService(coon);//取消服务isBind=false;}
4.生命周期
流程图