android----ServiceManager

serviceManager

在c++层如何使用serviceManager

frameworks\base\services\camera\tests\CameraServiceTest,示例代码如下:

sp<ICameraService>getCameraService(){

//取得serviceManager

sp<IServiceManager>sm=defaultServiceManager();

ASSERT(sm!=0);

sp<IBinder>binder=sm->getService(String16("media.camera"));

ASSERT(binder!=0);

sp<ICameraService>cs=interface_cast<ICameraService>(binder);

ASSERT(cs!=0);

returncs;

}

\frameworks\base\services\java\com\android\server\am

在java层使用serviceManager

\frameworks\base\services\java\com\android\server\am\

AtcitiveyManagerService.java示例代码:

publicstaticvoidsetSystemProcess(){

try{

//以下语句相当于上面c++层的,

//defaultServiceManager()->addService()

ActivityManagerServicem=mSelf;

ServiceManager.addService("activity",m);

ServiceManager.addService("meminfo",newMemBinder(m));

if(MONITOR_CPU_USAGE){

ServiceManager.addService("cpuinfo",newCpuBinder

(m));

}

ServiceManager.addService("activity.broadcasts",new

BroadcastsBinder(m));

ServiceManager.addService("activity.services",new

ServicesBinder(m));

ServiceManager.addService("activity.senders",new

SendersBinder(m));

ServiceManager.addService("activity.providers",

newProvidersBinder(m));

ServiceManager.addService("permission",

newPermissionController(m));

ApplicationInfoinfo=

mSelf.mContext.getPackageManager

().getApplicationInfo(

"android",

PackageManager.GET_SHARED_LIBRARY_FILES);

synchronized(mSelf){

ProcessRecordapp=mSelf.newProcessRecordLocked(

mSystemThread.getApplicationThread(),

info,

info.processName);

app.persistent=true;

app.pid=Process.myPid();

app.maxAdj=SYSTEM_ADJ;

mSelf.mProcessNames.put(app.processName,

app.info.uid,app);

synchronized(mSelf.mPidsSelfLocked){

mSelf.mPidsSelfLocked.put(app.pid,app);

}

mSelf.updateLRUListLocked(app,true);

}

}catch(PackageManager.NameNotFoundExceptione){

thrownewRuntimeException("Unabletofindandroidsystem

package",e);

}

}

serviceManager在c++层

\frameworks\base\libs\binder

IServiceManager.cpp文件部分代码如下:

sp<IServiceManager>defaultServiceManager()

{

if(gDefaultServiceManager!=NULL)returngDefaultServiceManager;

{

AutoMutex_l(gDefaultServiceManagerLock);

if(gDefaultServiceManager==NULL){

gDefaultServiceManager=interface_cast<IServiceManager>(

//产生一个bpServicManager对象,提代iServiceManager接口,让

defaultServiceManager来回调这个接口

ProcessState::self()->getContextObject(NULL));

}

}

returngDefaultServiceManager;

}

serviceManager在java层

\frameworks\base\core\java\android\os

publicfinalclassServiceManager{

.....

privatestaticIServiceManagergetIServiceManager(){

if(sServiceManager!=null){

returnsServiceManager;

}

//Findtheservicemanager

//产生一个serviceManagerProxy,提供IServiceManager接口,

getiServiceManager回调IServiceManager接口

sServiceManager=ServiceManagerNative.asInterface

(BinderInternal.getContextObject());

returnsServiceManager;

}

publicstaticIBindergetService(Stringname){

try{

IBinderservice=sCache.get(name);

if(service!=null){

returnservice;

}else{

//调用底层serviceManager

returngetIServiceManager().getService(name);

}

}catch(RemoteExceptione){

Log.e(TAG,"erroringetService",e);}

returnnull;

}

publicstaticvoidaddService(Stringname,IBinderservice){

try{

//调用底层serviceManager

getIServiceManager().addService(name,service);

}catch(RemoteExceptione){

Log.e(TAG,"errorinaddService",e);

}

}

.....

}

相关推荐