系列笔记17、代理模式-ActivityManagerProxy、Binder

来源:互联网 发布:淘宝店铺联盟在哪里 编辑:程序博客网 时间:2024/06/07 11:19

Android源码里的ActivityManagerProxy是代理模式的实现。


package android.app;
public abstract class ActivityManagerNative extends Binder implements IActivityManager
{
  class ActivityManagerProxy implements IActivityManager
  {
      ...
  }
}


ActivityManagerProxy相当于代理部分,实现了IActivityManager接口,该接口定义了一些Activity相关的接口方法。


package android.app;
public interface IActivityManager extends IInterface {
   public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,
            String resolvedType, IBinder resultTo, String resultWho, int requestCode, int flags,
            ProfilerInfo profilerInfo, Bundle options) throws RemoteException;
   public Intent registerReceiver(IApplicationThread caller, String callerPackage,
            IIntentReceiver receiver, IntentFilter filter,
            String requiredPermission, int userId) throws RemoteException;
   public ComponentName startService(IApplicationThread caller, Intent service,
            String resolvedType, String callingPackage, int userId) throws RemoteException;
   ...
}


IActivityManager接口相当于代理模式中的抽象主题,而真正实现主题的,则是继承自ActivityManagerNative的AMS。


public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
   ...
}


AMS属于系统级的Service并且运行于独立的进程空间中,可以通过ServiceManager来获取它,而ActivityManagerProxy也运行于自己所处的进程空间中,AMS与ActivityManagerProxy属于跨进程通信,实现是基于Android的Binder机制。ActivityManager是管理与维护Activity相关信息的类,其大多数逻辑实质上是由ActivityManagerProxy承担。




Android中的Binder机制与AIDL


传统的跨进程通信方式有:Socket、信号量、管道、内存共享、消息队列等





Binder所涉及的4个主要模块:Binder Client、Binder Server、ServerManager和Binder Driver。
Binder Client相当于客户端,Binder Server相当于服务器, ServerManager相当于DNS服务器,Binder Driver 相当于一个路由器。 











参考文章

http://blog.csdn.net/luoshengyang/article/details/6618363/














0 0
原创粉丝点击