当前位置:网站首页>AIDL总结
AIDL总结
2022-07-22 01:56:00 【dongpingwang】
监听服务端死亡
注意 onServiceDisconnected 只有在死亡意外死亡后才会回调,unbindService 不会回调
mServiceConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, final IBinder service) { Log.d(TAG, "onServiceConnected"); try { // 设置服务死亡代理,服务死亡后可自动绑定服务 service.linkToDeath(new IBinder.DeathRecipient() { @Override public void binderDied() { Log.d(TAG, "binderDied"); service.unlinkToDeath(this, 0); bindService(); } }, 0); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected"); playerService = null; } }
监听客户端死亡
private RemoteCallbackList<IMetadataUpdateCallback> callbacks = new RemoteCallbackList<IMetadataUpdateCallback>() { @Override public void onCallbackDied(IMetadataUpdateCallback callback) { super.onCallbackDied(callback); Log.d(TAG, "onCallbackDied:" + callback); } };
服务端收到的对象并不相同,BinderProxy是相同的
每个进程对应着一个VM,对象无法在进程间传递,binder机制数据传递是通过序列化进行的,
// 客户端 2022-07-16 15:38:39.900 9756-9756/com.example.aidl D/MainActivity: registerMetadataUpdateCallback: [email protected] asBinder:[email protected] // 服务端 2022-07-16 15:38:39.900 9789-9808/? D/PlayerService: registerMetadataUpdateCallback:[email protected] asBinder: [email protected] 2022-07-16 15:39:00.620 9789-9808/com.example.aidl:Remote D/PlayerService: unregisterMetadataUpdateCallback:[email protected] asBinder: [email protected]
RemoteCallbackList作用
1.以IBinder-Callback的键值对形式,存储Callback,
2.添加死亡代理监听,自动清除Callback
3.将map中value复制到数据中,来保证多线程安全
4.源码mCallbacks用来存储Callback,mActiveBroadcast在调用时保存mCallbacks.values,来保证多线程安全
ArrayMap < IBinder, Callback > mCallbacks = new ArrayMap < IBinder, Callback > (); private Object[] mActiveBroadcast; public int beginBroadcast() { synchronized(mCallbacks) { if(mBroadcastCount > 0) { throw new IllegalStateException("beginBroadcast() called while already in a broadcast"); } final int N = mBroadcastCount = mCallbacks.size(); if(N <= 0) { return 0; } Object[] active = mActiveBroadcast; if(active == null || active.length < N) { mActiveBroadcast = active = new Object[N]; } for(int i = 0; i < N; i++) { active[i] = mCallbacks.valueAt(i); } return N; } } public E getBroadcastItem(int index) { return((Callback) mActiveBroadcast[index]).mCallback; } public void finishBroadcast() { synchronized(mCallbacks) { if(mBroadcastCount < 0) { throw new IllegalStateException("finishBroadcast() called outside of a broadcast"); } Object[] active = mActiveBroadcast; if(active != null) { final int N = mBroadcastCount; for(int i = 0; i < N; i++) { active[i] = null; } } mBroadcastCount = -1; } }
权限校验
可以在Service中onBind方法返回Binder对象时进行校验或者在自定义Binder类中onTransact进行校验
<permission android:name="com.example.aidl.BookService" android:protectionLevel="signature" /> <uses-permission android:name="com.example.aidl.BookService" />
@Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); if(checkCallingOrSelfPermission(PERMISSION) == PackageManager.PERMISSION_DENIED) { Log.d(TAG, "Bind PlayerService need permission example.permission.TEST_AIDL, are you have?"); return null; } return mBinder; }
数据流向
in
基本数据类型只能为in,可以不用加
oneway 只能是in
out
inout
https://www.jianshu.com/p/31898ea2dfa3
oneway
binder默认是同步调用的,会挂起客户端直至完成,oneawy是异步调用
oneway 修饰的方法返回值只能是void
边栏推荐
- 2022-07-15 mysql/stone sub query memory copy analysis and optimization
- Conf configuration of redis
- 【社媒营销】进来了解下Whatsapp Business API?出海群发不用愁!
- shardingjdbc水平分表配置
- Paper reading | point voxel CNN for efficient 3D deep learning
- Worthington cell separation optimization system (including cell separation guide)
- 安卓 kotlin 的简单使用
- 发布时间测试
- 让安全动起来 | 甭管什么行业网络架构,这六招拿下靶标
- Simple use of Android kotlin
猜你喜欢
硅谷课堂笔记(中)
Worthington植物原生质体制备丨纤维素酶方案
Do you know who the "open source queen" is-- Facing the threat of being fired, she still insisted on opening a famous project
Study on the secretory enzymes of Worthington fungi -- galactose oxidase scheme
Spark summary
Redis master-slave replication
Industry analysis | logistics intercom
硅谷课堂笔记(下)
How to prolong the working life of slip ring
滑环的分类以及用途
随机推荐
RPC core module summary
Worthington真菌分泌酶研究 丨半乳糖氧化酶方案
NAT Technology
Worthington deoxyribonucleic acid and related research tools
Use expansion of redis persistence
2022-07-18 Jenkins deployment
How to solve the "last mile of delivery" of community group purchase
Voting cannot be repeated
Shardingjdbc horizontal split table configuration
IO 和NIO的区别
2022-07-18 eliminate traversal processing of exists subquery
等保合规2022系列 | 今年,关于等保你该了解什么?
配置Eureka时Status显示的是电脑名而不是localhost及ipAddr显示为本机ip的问题
Redis持久化(RDB和AOF)
JDBC编程
2022-07-12 use perf to count the performance of MySQL execution
Redis的拓展方案
Redis缓存穿透和雪崩
Various application schemes of animal free collagenase Worthington
MSTP&VRRP协议