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);
    return cs;
   }
\frameworks\base\services\java\com\android\server\am


  在java层使用serviceManager
   \frameworks\base\services\java\com\android\server\am\
   AtcitiveyManagerService.java 示例代码:
public static void setSystemProcess() { 
           try { 
                 //以下语句相当于上面c++层的,
                 //defaultServiceManager()->addService()


                 ActivityManagerService m = mSelf;
                 ServiceManager.addService("activity", m);


 
                ServiceManager.addService("meminfo", new MemBinder(m)); 
                if (MONITOR_CPU_USAGE) { 
                      ServiceManager.addService("cpuinfo", new CpuBinder


(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", 
                               new ProvidersBinder(m)); 
                ServiceManager.addService("permission", 
                              new PermissionController(m)); 
                ApplicationInfo info = 
                      mSelf.mContext.getPackageManager


().getApplicationInfo( 
                                 "android", 
                                 PackageManager.GET_SHARED_LIBRARY_FILES); 
                                 synchronized (mSelf) { 
                      ProcessRecord app = 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.NameNotFoundException e) { 
                throw new RuntimeException("Unable to find android system 


package", e); 
           } 
      } 


 
  


 serviceManager 在c++层 


\frameworks\base\libs\binder
 IServiceManager.cpp文件部分代码如下:
 
sp<IServiceManager> defaultServiceManager()
{
    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
   
    {
       AutoMutex _l(gDefaultServiceManagerLock);
       if (gDefaultServiceManager == NULL) {
           gDefaultServiceManager = interface_cast<IServiceManager>(
                //产生一个bpServicManager对象,提代iServiceManager接口,让


defaultServiceManager来回调这个接口
                ProcessState::self()->getContextObject(NULL));
       }
    }
   
   return gDefaultServiceManager;
}


 serviceManager 在 java层 
 \frameworks\base\core\java\android\os
 public final class ServiceManager {
       .....
    private static IServiceManager getIServiceManager() {
       if (sServiceManager != null) {
            return sServiceManager;
        }
     // Find the service manager
      //产生一个serviceManagerProxy,提供IServiceManager接口,


getiServiceManager回调IServiceManager接口
      sServiceManager = ServiceManagerNative.asInterface


(BinderInternal.getContextObject());
        return sServiceManager;
    }


    public static IBinder getService(String name) {
        try {
            IBinder service = sCache.get(name);
            if (service != null) {          
            return service;
           } else {
      //调用底层serviceManager
               return getIServiceManager().getService(name);
           }
      } catch (RemoteException e) {
            Log.e(TAG, "error in getService", e);        }
        return null;
    }


    public static void addService(String name, IBinder service) {
       try {
            //调用底层serviceManager
            getIServiceManager().addService(name, service);
        } catch (RemoteException e) {
           Log.e(TAG, "error in addService", e);
        }
    }






.....
}
posted @ 2012-09-18 00:33  retacn_yue  阅读(959)  评论(0编辑  收藏  举报