如何使用Binder类
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容介绍了"如何使用Binder类"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!aidl服务端定
千家信息网最后更新 2025年11月07日如何使用Binder类
本篇内容介绍了"如何使用Binder类"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
aidl
服务端定义接口
定义aidl文件,放在src目录下
实现编译aidl后的内部类stub
Android SDK 工具会基于您的 .aidl 文件,使用 Java 编程语言生成接口。此接口拥有一个名为 Stub 的内部抽象类,用于扩展 Binder 类并实现 AIDL 接口中的方法。您必须扩展 Stub 类并实现这些方法。
向客户端公开接口
实现 Service 并重写 onBind(),从而返回 Stub 类的实现。
客户端使用
使用bindService()
再src目录包含该aidl文件,为客户端提供使用adil权限
注意: 调用是同步的,如果服务耗时需要再子线程调用,需要考虑线程安全
传递类
目前只能传递基本类型数据,传递对象类型需要model类实现Parcelable
调用方始终捕获DeadObjectException异常
DeadObjectException
代码示例
// IRemoteService.aidlpackage com.example.android// Declare any non-default types here with import statements/** Example service interface */internal interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ val pid:Int /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ fun basicTypes(anInt:Int, aLong:Long, aBoolean:Boolean, aFloat:Float, aDouble:Double, aString:String)}编译后生成IRemoteService extends Binder implements IMyService{ class Stub extends android.os.Binder implements IMyService{ // 客户端拿到Binder获得一个Proxy代理对象 private static class Proxy implements IMyService{ } }}// 向客户端公开接口class RemoteService : Service() { override fun onCreate() { super.onCreate() } override fun onBind(intent: Intent): IBinder { // Return the interface return binder }// 实现stub privateval binder = object : IRemoteService.Stub() { override fun getPid(): Int { return Process.myPid() } override fun basicTypes( anInt: Int, aLong: Long, aBoolean: Boolean, aFloat: Float, aDouble: Double, aString: String ) { // Does nothing } }}var iRemoteService: IRemoteService? = nullval mConnection = object : ServiceConnection { // Called when the connection with the service is established override fun onServiceConnected(className: ComponentName, service: IBinder) { // Following the example above for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use to call on the service iRemoteService = IRemoteService.Stub.asInterface(service) } // Called when the connection with the service disconnects unexpectedly override fun onServiceDisconnected(className: ComponentName) { Log.e(TAG, "Service has unexpectedly disconnected") iRemoteService = null }}绑定服务
1、扩展binder类,常规service写法。非夸进程,用于后台服务处理 2、Messenger处理跨进程。特点是不需要考虑多线程安全,它是通过handler维护的messenger会创建所有请求的队列。使用简单 3、aidl,跨进程需要考虑多线程问题。设计相对复杂
// messenger服务创建/** Command to the service to display a message */private const val MSG_SAY_HELLO = 1class MessengerService : Service() { /** * Target we publish for clients to send messages to IncomingHandler. */ private lateinit var mMessenger: Messenger /** * Handler of incoming messages from clients. */ internal class IncomingHandler( context: Context, privateval applicationContext: Context = context.applicationContext ) : Handler() { override fun handleMessage(msg: Message) { when (msg.what) { MSG_SAY_HELLO -> Toast.makeText(applicationContext, "hello!", Toast.LENGTH_SHORT).show() else -> super.handleMessage(msg) } } } /** * When binding to the service, we return an interface to our messenger * for sending messages to the service. */ override fun onBind(intent: Intent): IBinder? { Toast.makeText(applicationContext, "binding", Toast.LENGTH_SHORT).show() mMessenger = Messenger(IncomingHandler(this)) return mMessenger.binder }}"如何使用Binder类"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
接口
客户
客户端
服务
线程
文件
安全
内容
对象
方法
更多
目录
知识
类型
处理
编译
复杂
实用
学有所成
接下来
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
医疗设备的网络安全
关于大学生网上订餐的数据库
西安淘花园网络技术有限公司
华为2448h 服务器
安全狗影响服务器吗
网络安全防护设备兼容性
联合国网络安全政府专家组
工业软件开发公司怎样跑业务
sql数据库用什么软件
国内服务器安全
图腾机柜型号 服务器
网络安全监测装置图纸
浙江综合软件开发产业
做软件开发和算法哪个累
软件开发岗位职业要求
长安区通通网络技术服务部
关于网络安全实训报告作文
杭州马某网络安全
数据库将R分解成3NF
国家网络安全公务员多少钱
为什么授权码需要连接服务器
密码编码学与网络安全学习
远程网络安全知识
数据库设计员工培训管理系统
雷州市网络安全和信息化委员会
在数据库中创建表格的三原则
数据库怎么弄系统界面
阳江通讯软件开发供应商家
运维可以做软件开发嘛
数据库收缩时间