Android上下文服务管理查询过程

原创 2013年12月05日 16:34:59

在开发Android应用程序时,如果需要使用系统提供的服务,可以通过服务名称调用山下文的getSystemService(String name)来获取服务管理者,那么该函数是如何实现服务查询的呢?

frameworks\base\core\java\android\app\ContextImpl.java

public Object getSystemService(String name) {
	ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
	return fetcher == null ? null : fetcher.getService(this);
}
SYSTEM_SERVICE_MAP被定义为HashMap类型变量,该变量以服务名称—ServiceFetcher对象这种键值对的方式保存了一系列系统服务管理对象,该变量定义为静态类型,因此在ContextImpl类加载时,该变量就已经创建,同时在ContextImpl类中通过Static块语句来初始化SYSTEM_SERVICE_MAP变量:

static {
	registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
			public Object getService(ContextImpl ctx) {
				return AccessibilityManager.getInstance(ctx);
			}});

	registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
				IAccountManager service = IAccountManager.Stub.asInterface(b);
				return new AccountManager(ctx, service);
			}});

	if (SystemProperties.getBoolean("universe_ui_support",false)) {
		registerService(THEME_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
			IBinder b = ServiceManager.getService(THEME_SERVICE);
			IThemeManager service = IThemeManager.Stub.asInterface(b);
			return new ThemeManager(ctx, service);
			}});
	}

	registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
			}});

	registerService(ALARM_SERVICE, new StaticServiceFetcher() {
			public Object createStaticService() {
				IBinder b = ServiceManager.getService(ALARM_SERVICE);
				IAlarmManager service = IAlarmManager.Stub.asInterface(b);
				return new AlarmManager(service);
			}});

	registerService(AUDIO_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new AudioManager(ctx);
			}});

	registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new MediaRouter(ctx);
			}});

	registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new ClipboardManager(ctx.getOuterContext(),
						ctx.mMainThread.getHandler());
			}});

	registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
			public Object createStaticService() {
				IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
				return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
			}});

	registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
			public Object createStaticService() {
				IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
				return new CountryDetector(ICountryDetector.Stub.asInterface(b));
			}});

	registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
			}});

	registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
			}});

	registerService(NFC_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new NfcManager(ctx);
			}});

	registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
			public Object createStaticService() {
				return createDropBoxManager();
			}});

	registerService(INPUT_SERVICE, new StaticServiceFetcher() {
			public Object createStaticService() {
				return InputManager.getInstance();
			}});

	registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return InputMethodManager.getInstance(ctx);
			}});

	registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return TextServicesManager.getInstance();
			}});

	registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
			public Object getService(ContextImpl ctx) {
				// TODO: why isn't this caching it?  It wasn't
				// before, so I'm preserving the old behavior and
				// using getService(), instead of createService()
				// which would do the caching.
				return new KeyguardManager();
			}});

	registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
			}});

	registerService(LOCATION_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(LOCATION_SERVICE);
				return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
			}});

	registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
		@Override
		public Object createService(ContextImpl ctx) {
			return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
					ServiceManager.getService(NETWORK_POLICY_SERVICE)));
		}
	});

	registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				final Context outerContext = ctx.getOuterContext();
				return new NotificationManager(
					new ContextThemeWrapper(outerContext,
							Resources.selectSystemTheme(0,
									outerContext.getApplicationInfo().targetSdkVersion,
									com.android.internal.R.style.Theme_Dialog,
									com.android.internal.R.style.Theme_Holo_Dialog,
									com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
					ctx.mMainThread.getHandler());
			}});

	registerService(NSD_SERVICE, new ServiceFetcher() {
			@Override
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(NSD_SERVICE);
				INsdManager service = INsdManager.Stub.asInterface(b);
				return new NsdManager(ctx.getOuterContext(), service);
			}});

	// Note: this was previously cached in a static variable, but
	// constructed using mMainThread.getHandler(), so converting
	// it to be a regular Context-cached service...
	registerService(POWER_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(POWER_SERVICE);
				IPowerManager service = IPowerManager.Stub.asInterface(b);
				return new PowerManager(service, ctx.mMainThread.getHandler());
			}});

	registerService(SEARCH_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new SearchManager(ctx.getOuterContext(),
						ctx.mMainThread.getHandler());
			}});

	registerService(SENSOR_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper());
			}});

	registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new StatusBarManager(ctx.getOuterContext());
			}});

	registerService(STORAGE_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				try {
					return new StorageManager(ctx.mMainThread.getHandler().getLooper());
				} catch (RemoteException rex) {
					Log.e(TAG, "Failed to create StorageManager", rex);
					return null;
				}
			}});
	registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new TelephonyManager(ctx.getOuterContext());
			}});

	registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
			public Object createStaticService() {
				IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
				return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
			}});

	registerService(UI_MODE_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new UiModeManager();
			}});

	registerService(USB_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(USB_SERVICE);
				return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
			}});

	registerService(SERIAL_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(SERIAL_SERVICE);
				return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
			}});

	registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				return new SystemVibrator();
			}});

	registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);

	registerService(WIFI_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(WIFI_SERVICE);
				IWifiManager service = IWifiManager.Stub.asInterface(b);
				return new WifiManager(service, ctx.mMainThread.getHandler());
			}});

	registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
			public Object createService(ContextImpl ctx) {
				IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
				IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
				return new WifiP2pManager(service);
			}});

	registerService(WINDOW_SERVICE, new ServiceFetcher() {
			public Object getService(ContextImpl ctx) {
				return WindowManagerImpl.getDefault(ctx.mPackageInfo.mCompatibilityInfo);
			}});
}
在该Static块语句里,通过调用registerService函数向SYSTEM_SERVICE_MAP哈希变量中添加服务管理对象:

private static void registerService(String serviceName, ServiceFetcher fetcher) {
	if (!(fetcher instanceof StaticServiceFetcher)) {
		fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
	}
	SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
}
首先通过服务名称从SYSTEM_SERVICE_MAP哈希表中查找出服务对应的ServiceFetcher对象,然后调用ServiceFetcher对象的getService函数来获取服务管理Manager对象
public final Object getService(ContextImpl unused) {
	synchronized (StaticServiceFetcher.this) {
		Object service = mCachedInstance;
		if (service != null) {
			return service;
		}
		return mCachedInstance = createStaticService();
	}
}
这里以单例模式获取服务管理对象,如果变量mCachedInstance为空,则表示系统还未使用过该服务,则调用createStaticService()函数来构造服务管理对象,该函数是一个抽象函数,在注册每一个服务管理对象时实现该函数,在该函数里构造对应的服务管理对象。


相关文章推荐

IT服务管理(ITSM)实施过程

IT服务管理(ITSM)实施的理想状态是能够快速提升企业的竞争力。但是,从国内众多实施ITSM的企业来看,失败的例子比比皆是。 企业应如何合理有效地选择和利用多达11个核心流程的ITSM理论,以...
  • aolaog
  • aolaog
  • 2011年05月04日 17:38
  • 588

第一帖来谈谈运维服务管理过程中的制高点:系统性调优

运维服务的重要性在今天已经越来越得到大家的认同了。但是尽管大家都知道它重要,却往往陷入只有苦劳、难见功劳的困境中。的确,相比起在一张白纸上画图的集成,运维不是那么好出成绩。不过,不好出成绩并不代表不能...

Android实现电蚊香/Service服务管理类

实现这个实例需要的就是Service这个类的管理,我们用到的是启动Service,并在退出应用程序的时候关闭(Stop)Service,下面我们首先看下这个程序的运行截图: 图中显示的控件一...

Android 之 ServiceManager与服务管理

ServiceMananger是android中比较重要的一个进程,它是在init进程启动之后启动,从名字上就可以看出来它是用来管理系统中的service。比如:InputMethodService、...
  • xieqibao
  • xieqibao
  • 2011年07月05日 10:44
  • 33263

Android入门进阶教程(13)-ServiceManager服务管理详解

ServiceMananger是android中比较重要的一个进程,它是在init进程启动之后启动,从名字上就可以看出来它是用来管理系统中的service。比如:InputMethodService、...

Android中服务管理相关流程getSystemService

我们经常会使用context来获取服务实例,完成某些特定的功能。 辣么他的大致是什么样的流程呢?下面跟踪一下source code。 一、相关的类文件: framework/base/co...

第十五篇 Android 的Backup服务管理机制--助手模式

Android2.2以后的备份服务功能可以允许用户备份应用数据到云存储中,当应用执行了工厂恢复服务或者转换到一个新的平台上时,如果备份的应用重新安装,系统就自动恢复原先备份的数据,这个过程对用户是完全...
  • twlkyao
  • twlkyao
  • 2013年03月28日 22:28
  • 918

第十五篇 Android 的Backup服务管理机制与架构分析

Android2.2以后的备份服务功能可以允许用户备份应用数据到云存储中,当应用执行了工厂恢复服务或者转换到一个新的平台上时,如果备份的应用重新安装,系统就自动恢复原先备份的数据,这个过程对用户是完全...
  • GooHong
  • GooHong
  • 2012年09月27日 18:49
  • 10467

Android 的Backup服务管理机制与架构分析

Android2.2以后的备份服务功能可以允许用户备份应用数据到云存储中,即当应用执行了工厂服务或转换到一个新的平台上时,如果备份的应用需要重新安装,系统就自动恢复原先备份的数据,这个过程对用户是完全...

Android的Context Manager(服务管理器)源码剖析-android学习之旅(99)

Context Manager介绍Context Manager对应的进程是servicemanager进程,它先于Service Server和服务客户端运行,进入接收IPC数据的待机状态,处理来自...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android上下文服务管理查询过程
举报原因:
原因补充:

(最多只允许输入30个字)