Android进程中通信的方式

本文详细介绍了Android中进程间通信的多种方式,包括使用进程共享、广播、AIDL、Message+Binder、Socket以及ContentProvider。重点讲解了Binder机制,解释了为何Binder适合于Android系统的跨进程通信,以及Binder在数据拷贝、安全性、稳定性的优势。同时,文章提到了TransactionTooLargeException异常的产生原因和解决方案,并探讨了Zygote使用Socket而非Binder的原因。
摘要由CSDN通过智能技术生成

     本文来自刘兆贤的博客_CSDN博客-Java高级,Android旅行,Android基础领域博主 ,引用必须注明出处!

大的方向上

第一、使用进程共享的方式,往往使用android:process=remote,这样开启一个新的进程,使得所有进程都可以访问这个进程,使服务可以在多进程共享;而android:process=:remote相当于给当前进程一个私有进程,用来维护其自身的业务处理。开启新进程可以用在activity、service、broadcastReceiver、ContentProvider等组件。

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process.  If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

第二、使用广播的方式,可以设置category、component、package来区别广播,同时使用自定义权限来做限制

方法1、使用AIDL的方式进行

第三、使用Message+Binder

第四、使用Socket

第五、共享内存,如ContentProvider

linux有管道(Pipe)、信号(Signal)、报文(Message)、Trace、ShareMemory等。

Binder能支持跨进程通信的原因:是它实现IBinder接口,系统定义实现此接口即赋予进程通信的功能。

优势:做数据拷贝只用一次,则Pipe、Socket都需要两次;其次安全性高,不会像Socket会暴露地址,被人替换;

通信机制:Service向ServiceManager注册,得到虚拟的Uid和Pid;Client向ServiceManager请求,得到虚拟的Uid和Pid,以及目标对象的Proxy,底层通过硬件协议传输,使用Binder通讯。

通信时Client手持Proxy,ServiceManger进行转换,调用到Service。三者运行在三个独立进程中。Client/Sever全双工,互为Sever/Client。<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘兆贤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值