一.输入:

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.创建绑定服务


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


流程图

二.输出:

android studio BindService相关推荐

  1. android studio 跨进程,Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用...

    本文首发于微信公众号「后厂技术官」 在上一篇文章Android IPC机制(二)用Messenger进行进程间通信中我们介绍了使用Messenger来进行进程间通信的方法,但是我们能发现Messeng ...

  2. Android Studio 导入OpenCV 并调试运行face-detection例子

    系统:Ubuntu 14.04 Studio版本:2.3.3 OpenCV版本:2.4.11 第一部分 将OpenCV导入到项目中 1.从Google Android 开发中文网站上下载 Androi ...

  3. Android Studio实现音乐播放器2.0

    项目目录 一.引言 二.项目概述 1.需求分析 2.设计分析 3.资源文件分析 三.开发环境 四.优化设计 1.上一首下一首 2.个性化按钮 五.运行效果 六.项目总结 七.源码获取 一.引言 我在一 ...

  4. Android Studio使用AIDL技术进行SDK开发

    前面我们有介绍AIDL的基本用法: Android进程间通信--AIDL Android进程间通信--AIDL Binder连接池 现在我们来介绍利用AIDL来实现一个简陋的SDK,将获取用户信息的方 ...

  5. android studio怎么改软件扫码界面_一文入门Android逆向

    本文节主要介绍一下Android逆向常用的环境.工具.动静态分析思路,笔者通过学习肉丝大佬分享的一些内容,加上自己一些经验总结而来. 1.环境准备 环境也分三六九等,一个好的环境能让你节省大量时间和精 ...

  6. 《Android移动应用基础教程》(Android Studio)(第二版)黑马程序员 课后习题答案

    <Android移动应用基础教程>(Android Studio)(第二版)黑马程序员 课后习题答案 目录 第1章 Android基础入门 第2章 Android常见界面布局 第3章 An ...

  7. android studio中使用AIDL进行客户端与服务端互相通信

    前言 在AIDL实现IPC通信,调用远程服务端的方法.但是,远程服务端并不能主动给客户端返回信息.在很多情况下是需要远程服务端主动给客户端返回数据,客户端只需要进行监听即可,这是典型的观察者模式.这篇 ...

  8. Android Studio如何实现音乐播放器(简单易上手)

    我们大家平时长时间打代码的时候肯定会感到疲惫和乏味,这个时候一边播放自己喜欢的音乐,一边继续打代码,心情自然也愉快很多.音乐带给人的听觉享受是无可比拟的,动听的音乐可以愉悦人的身心,让人更加积极地去热 ...

  9. Android Studio 实现音乐播放器

    目录 一.引言 视频效果展示: 图片效果展示: .启动页效果 2.登录页效果 3.注册页效果 4.歌曲列表页效果 5.播放页效果 二.详细设计 1.登陆注册功能 2.音乐列表页面 2.音乐播放功能 一 ...

最新文章

  1. PHP使用CURL案例
  2. 一篇文章带你搞懂前端面试技巧及进阶路线
  3. 《COM原理与应用》学习笔记二——COM对象和COM接口的实现
  4. linux内核disabled,Linux内核关闭IPv6协议的方式
  5. PyTorch实战福利从入门到精通之七——卷积神经网络(LeNet)
  6. python 用mysqldb方式操作数据库
  7. ros先订阅后发布 无法收到消息的解决办法
  8. 在linux系统下安装redis
  9. 标准C语言第四版答案第十章,谭浩强C语言 第十章课后习题
  10. 关于win10优化问题
  11. abc计算机机房建设标准,ABC级数据中心机房建设要求
  12. C4D和Maya哪个学起来更容易
  13. [清新]黄花菜花盛开的季节
  14. jsp汽车销售系统带前端
  15. post和get传参(重点)
  16. 1-3分钟教你如何开通微信支付0.2%费率,适用于公众号小程序和收款码
  17. JVM Tomcat性能实战
  18. 移动常见业务单词和词组
  19. 关于xlrd.biffh.XLRDError: Excel xlsx file; not supported报错问题的两种解决方案
  20. 数据库第一范式、第二范式、第三范式、BCNF范式

热门文章

  1. 分集接收技术性能MATLAB,基于Matlab分集接受性能仿真 .doc
  2. 暴雪战网服务器维护时间,炉石传说停机维护30小时 暴雪与网易做了个艰难的决定...
  3. LCD1602的使用方法
  4. 香港真缺人了!又一间与内地合作的联合实验室成立!
  5. 第十一届蓝桥杯JavaB组省赛(题目及AC题解)
  6. 字典中由value查key的几点说明
  7. NFT行业周报——2022年5月第一期
  8. 路由器的搭建以及实现虚拟机上网
  9. JavaScript--DOM-- firstChild 和lastChild 属性
  10. 医疗信息系统等保定级备案教程