如何使用Binder类
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,本篇内容介绍了"如何使用Binder类"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!aidl服务端定
千家信息网最后更新 2025年02月24日如何使用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安全错误
数据库的锁怎样保障安全
学校网络技术员的工资待遇
地产软件开发平台
网络安全法是哪个部门颁布的
数据库表连接取值范围
手机游戏软件开发制作
浙江网络软件开发定制包括什么
java mail服务器
gsm网络技术发短信
网络安全改造售后服务方案
苏州erp软件开发代码
服务器f恢复
软件开发流程DR
重庆贷款软件开发公司有哪些
渗透测试还是软件开发
调研企业网络安全情况
网络安全恶性行为检测
长春直销软件开发
sybase 到处数据库
中国台湾文档软件开发定制
呼市网络安全知识
琼海软件开发文档
地产软件开发平台
服务器关了会网络连接超时吗
数据库表中去除重复数据
服务器跑python模型
关于获取数据库的说明范文
网络安全宣传册下载
红警玩家数据库
仙居本地软件开发价格行情
sql数据库删除后恢复