android studio开发中如何 BindService
程序员文章站
2022-06-28 17:05:25
代码:主界面代码: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.创建绑定服务
-
实现这些方法: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
上一篇: 处暑前后吃什么比较对身体好?
推荐阅读
-
如何在Android中实现左右滑动的指引效果
-
如何在Android中实现渐显按钮的左右滑动效果
-
在Office2010中如何设置开启“开发工具”功能选项卡
-
Android Studio中引入Lambda表达式的方法
-
Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
-
android开发中的文件路径详情
-
解析如何在android中增加gsensor驱动(MMA7660)
-
Android Studio代码中的关键字怎么设置颜色?
-
Android编程开发中的正则匹配操作示例
-
如何设置Android studio 3.0显示光标返回上一次浏览位置的箭头图标