位置: frameworks/base/core/java/android/os/ServiceManager.java
介绍:framework 核心类,保存所有service的 binder引用,提供service添加和查询接口。
主要接口:
public static void addService(String name, IBinder service) {
try {
getIServiceManager().addService(name, service, false);
} catch (RemoteException e) {
Log.e(TAG, "error in addService", e);
}
}
public static IBinder getService(String name) {
try {
IBinder service = sCache.get(name);
if (service != null) {
return service;
} else {
return getIServiceManager().getService(name);
}
} catch (RemoteException e) {
Log.e(TAG, "error in getService", e);
}
return null;
}
主要使用地方:
1. framework/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
...
ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
ServiceManager.addService("telephony.registry", telephonyRegistry);
ServiceManager.addService("vibrator", vibrator);
ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
new AccessibilityManagerService(context));
ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
ServiceManager.addService(Context.CLIPBOARD_SERVICE,
new ClipboardService(context));
ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
ServiceManager.addService(Context.NETWORK_SCORE_SERVICE, networkScore);
ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
ServiceManager.addService(
Context.NSD_SERVICE, serviceDiscovery);
ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
new UpdateLockService(context));
ServiceManager.addService(Context.LOCATION_SERVICE, location);
ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
ServiceManager.addService(Context.SEARCH_ENGINE_SERVICE,
new SearchEngineManagerService(context));
ServiceManager.addService(ISensorHubManager.SENSORHUB_SERVICE,
new SensorHubService(context));
ServiceManager.addService(Context.SERIAL_SERVICE, serial);
ServiceManager.addService(Context.HARDWARE_PROPERTIES_SERVICE,
hardwarePropertiesService);
ServiceManager.addService("diskstats", new DiskStatsService(context));
ServiceManager.addService("samplingprofiler",
new SamplingProfilerService(context));
ServiceManager.addService("network_time_update_service", networkTimeUpdater);
ServiceManager.addService("commontime_management", commonTimeMgmtService);
ServiceManager.addService(AssetAtlasService.ASSET_ATLAS_SERVICE, atlas);
ServiceManager.addService(GraphicsStatsService.GRAPHICS_STATS_SERVICE,
new GraphicsStatsService(context));
ServiceManager.addService(Context.MEDIA_ROUTER_SERVICE, mediaRouter);
...
}
private void startBootstrapServices() {
...
// Start the package manager.
traceBeginAndSlog("StartPackageManagerService");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
// Set up the Application instance for the system process and get started.
...
mActivityManagerService.setSystemProcess();
...
}
...
2. frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
/// M: ANRManager mechanism @{
ServiceManager.addService("anrmanager", mANRManager, true);
/// @}
........
}
3. frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
public static PackageManagerService main(Context context, Installer installer,
boolean factoryTest, boolean onlyCore) {
// Self-check for initial settings.
PackageManagerServiceCompilerMapping.checkProperties();
PackageManagerService m = new PackageManagerService(context, installer,
factoryTest, onlyCore);
m.enableSystemUserPackages();
ServiceManager.addService("package", m);
return m;
}