Android Messager的使用
在Android系统的IPC通讯中,其中以Binder尤为重要,即可在native层玩转,也可在java层有余.
对于要讨论的Messager通讯,应该属于在研究系统时的意外发现,毕竟它不是很常用,也很少看到。
这个功能要从SystemUI的解图功能开始:
请留意图中的Binder,即整个Messager通讯机制也是建立在Binder之上。
来看看A进程的IMessage是怎么传过来的:
对的,就是在Onbind的时候传过来的。
看看API文档:
Messenger
public final class Messenger
extends Object
implements Parcelable
java.lang.Object | |
↳ | android.os.Messenger |
Reference to a Handler, which others can use to send messages to it. This allows for the implementation of message-based communication across processes, by creating a Messenger pointing to a Handler in one process, and handing that Messenger to another process.
Note: the implementation underneath is just a simple wrapper around a Binder
that is used to perform the communication. This means semantically you should treat it as such: this class does not impact process lifecycle management (you must be using some higher-level component to tell the system that your process needs to continue running), the connection will break if your process goes away for any reason, etc.
也就只有这么些。
当然如果要问他的局限性,那么就是Message的局限性,限制了大数据的传递,而且当没有很好的错误处理机制:
文档里面说的也很明了,没有生命周期管理,你需要在把你的进程更高的优先级使用它,如果你的进程死掉,那么他们之间的通讯也会端掉的。