SystemServer进程是由Zygote进程孵化的第一个进程,大部分Android提供的服务都在该进程中,SystemServer中运行的进程公共有六十多种,主要包括:ActivityManagerService(AMS),WindowManagerService(WMS),PackagManagerService(PMS)等;这些系统服务都是以一个线程的方式存在Systemserver进程中。
ActivityManagerService简称AMS,它是android中很重要的一个服务,它统筹管理着android的四大组件,统一调度各应用进程。
ActivityThread管理应用进程的主线程的执行(相当于普通Java程序的main入口函数),并根据AMS的要求(通过IApplicationThread接口,AMS为Client、ActivityThread内部类ApplicationThread为Server)负责调度和执行activitiy、service、broadcast和其它操作。
Android Binder机制浅析讲述了Binder机制的原理和在AIDL的应用;
App进程与SystemServer进程通信
APP进程通过Binder跨进程调用SystemServer进程的ActivityManagerService的具体调用流程和相关类,以startService为例来说明;
APP进程和SystemServer进程通信,APP进程是Client端,SystemServer进程是Server端;
ContextImpl的startServiceCommon()方法
private ComponentName startServiceCommon(Intent service, boolean requireForeground,
UserHandle user) {
ComponentName cn = ActivityManager.getService().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), requireForeground,
return cn;
}
ActivityManager的getService()获取IActivityManager对象实例,通过IActivityManager调用startService(),由其实现类ActivityManagerService的startService()方法实现具体业务逻辑;
现在关注getService()方法如何获取到对应的Service的?
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
//Singleton类常见IActivityManager的单例对象;作为Client根据获取对应的Service,
private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
//ServiceManager通过名称获取对应的Service;
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
//将IBinder对象转换成对应的接口对象;
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
ServiceManager类最主要的功能是addService()和getService();addService()主要是通过SystemServer类调用,向系统注册服务;getService()主要是通过类名查询对应的Service;
ServiceManager类中没有具体的操作,只是通过getIServiceManager()方法获取IServiceManager接口对象,由其具体的子类来实现。
sServiceManager对象是由ServiceManagerNative的asInterface()获取;
public final class ServiceManager {
private static final String TAG = "ServiceManager";
private static IServiceManager sServiceManager;
//key为Service的名称,value为IBinder对象;
private static HashMap<String, IBinder> sCache = new HashMap<String, IBinder>();
//创建IServiceManager对象;
private static IServiceManager getIServiceManager() {
if (sServiceManager != null) {
return sServiceManager;
}
// Find the service manager
sServiceManager = ServiceManagerNative
.asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));
return sServiceManager;
}
//如果sCache中存在,则直接返回;否则通过IServiceManager的getService()来获取;
public static IBinder getService(String name) {
try {
IBinder service = sCache.get(name);
if (service != null) {
return service;
} else {
return Binder.allowBlocking(getIServiceManager().getService(name));
}
} catch (RemoteException e) {
Log.e(TAG, "error in getService", e);
}
return null;
}
//调用IServiceManager的addService()来添加;
public static void addService(String name, IBinder service) {
try {
getIServiceManager().addService(name, service, false);
} catch (RemoteException e) {
Log.e(TAG, "error in addService", e);
}
}
}
IServiceManager接口的具体实现类为抽象类ServiceManagerNative和ServiceManagerProxy(在ServiceManagerNative内中),看过AIDL之后,我感觉应该还有继承IServiceManager.Stub的子类实现具体的逻辑,但没有找到。
ServiceManagerNative的asInterface()
public abstract class ServiceManagerNative extends Binder implements IServiceManager
{
//将IBinder对象转换为对应的Interface类;如果queryLocalInterface()返回为null,则创ServiceManagerProxy对象;
static public IServiceManager asInterface(IBinder obj)
{
if (obj == null) {
return null;
}
IServiceManager in =
(IServiceManager)obj.queryLocalInterface(descriptor);
if (in != null) {
return in;
}
return new ServiceManagerProxy(obj);
}
}
看下IServiceManager接口addService()和getService()的子类调用;由Proxy的getService()和addService()方法来实现,并且这两个方法的流程一致;
Proxy的getService()主要做了1)从对象池中取出对象Parcel的实例data和reply;2)往data中写入数据;调用ServiceManagerNative的transact()方法;3)读取ServiceManagerNative的transact()方法往reply中写入的数据,返回;
class ServiceManagerProxy implements IServiceManager {
public ServiceManagerProxy(IBinder remote) {
mRemote = remote;
}
public IBinder asBinder() {
return mRemote;
}
//1)从对象池中取出对象Parcel的实例data和reply;
//2)往data中写入数据;调用ServiceManagerNative的transact()方法;
//3)读取ServiceManagerNative的transact()方法往reply中写入的数据,返回;
public IBinder getService(String name) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IServiceManager.descriptor);
data.writeString(name);
mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0);
IBinder binder = reply.readStrongBinder();
reply.recycle();
data.recycle();
return binder;
}
private IBinder mRemote;
}
ServiceManagerNative的onTransact():读取data中的数据,调用getService()方法,将返回值写入reply中;
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
{
try {
switch (code) {
//读取data中的数据,调用getService()方法,将返回值写入reply中;
case IServiceManager.GET_SERVICE_TRANSACTION: {
data.enforceInterface(IServiceManager.descriptor);
String name = data.readString();
IBinder service = getService(name);
reply.writeStrongBinder(service);
return true;
}
}
} catch (RemoteException e) {
}
return false;
}
SystemServer进程和APP进程通信
SystemServer进程和APP进程通信,SystemServer进程是Client端,APPAPP进程是Server端;
剩下的具体流程和上面的类似,就不再详细分析了。
以上就是APP进程和SystemServer进程通信的主要内容,如有问题,请多指教,谢谢!