Android Binder 机制是安卓应用运行的基础,一个应用的运行需要无法避免地和系统提供的 Binder 做交互。Android 系统以服务(Service)的方式暴露出很多 Binder 对象,准确的说我们拿到的是 Binder 代理对象(BinderProxy),真正的 Binder 对象运行于安卓系统进程中(system_process)。我们的应用以夸进程的方式调用系统提供的各种服务,通常以 Context.getSystemService() 的方式获取系统服务,常见的有 ActivityManager, AlarmManager, InputMethodManager, ConnectivityManager, LayoutInflater 等等,其中有一部分是普通对象,大部分是对系统 Binder 对象的封装。那应用又是如何拿到系统的 Binder 对象的呢?如果我们要拿到其他应用进程的 Binder 对象一般会使用 ServiceConnection 连接其他进程的 Service 拿到 IBinder。然而系统的 IBinder 是用 ServiceManager 暴露给应用进程的。下面以获取 InputMethodManager 为例分析应用是如何获取系统 IBinder 对象的。