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()函数来构造服务管理对象,该函数是一个抽象函数,在注册每一个服务管理对象时实现该函数,在该函数里构造对应的服务管理对象。


Android 常用系统服务 学习总结

清明小长假,小丸子浪的已经不知自己是一个要上班的孩子~来一张海边游妹子合照 好啦我不是要来写旅游传记的啦,接下来就来进行常用系统服务的学习。 1、什么是SystemService? Androi...
  • Android_Glimmer
  • Android_Glimmer
  • 2016年04月05日 09:08
  • 1154

手机系统ROM开发中的电话号码格式客制化

需求描述: 在华为手机海外定制的需求中,针对电话号码的格式显示诉求,各地区有所不同,比如针对委瑞内拉地区需要按4-4-3格式化 实现细节: ...
  • lipengshiwo
  • lipengshiwo
  • 2017年06月03日 12:51
  • 400

Android获取上下文几个方法的区别

先看下继承关系,Activity/Service/Application都是继承自Context的,获取上下文实际获取的是各子类的上下文实例,可能是Activity,也可能是Application等,...
  • ethanhola
  • ethanhola
  • 2017年02月13日 11:46
  • 1271

Android中最常使用的Context上下文相关总结

其实在学习Android的时候,遇到上下文这个概念的时候,非常的迷糊,因为contex的种类太多了,什么getBaseContext(),  getApplicationContext().........
  • lanxingfeifei
  • lanxingfeifei
  • 2016年02月16日 15:36
  • 1704

Spring mvc 上下文初始化过程

在软件开发的中,如果某些特性的使用比较普遍,那么这些特性往往可以作为平台特性来实现,通过对这些平台特性进行有效的封装,使其向其他应用开放。正是如此,Spring由于其IOC、AOP、事务处理、持久化驱...
  • and1kaney
  • and1kaney
  • 2016年04月21日 23:59
  • 5022

Android上下文——两种上下文的对比 getApplicationContext()与Activity.this的浅认识

在Android application开发中,我们经常使用Context上下文对象,Context上下文对象是代表当前运行环境的组件的上下文,是一种环境的定义。 在Android中有两种方式表...
  • probuing
  • probuing
  • 2015年11月15日 23:08
  • 662

Android Context应用上下文详解

Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service、BroadcastReceiver、Activity等都会利用到Context的相关方法 ; 说它陌生,完全是...
  • cswhale
  • cswhale
  • 2014年08月31日 10:23
  • 8568

Context上下文理解及获得上下文的几种方式

Context理解Context提供了一个应用的运行环境,在Context的大环境里,应用才以访问资源,才能完成和其他组件、服务的交互,Context定义了一套基本的功能接口,我们可以理解为一套规范,...
  • cnsfz
  • cnsfz
  • 2016年11月20日 15:48
  • 485

android 获取全局上下文

有时我们在除activity外的代码里,我们也需要一个上下文,虽然我们也可以通过传值的形式把上下文传过来,但是这样做会是你的代码的参数增多,而且没遇到这样的情况你都的传一个上下文,这样就很麻烦,所以我...
  • louyxlovess
  • louyxlovess
  • 2016年01月26日 13:52
  • 1411

Linux学习笔记-----Linux服务管理----讲12.1 Linux服务管理-----服务分类

Linux服务管理 讲12.1  Linux服务管理-----服务分类 Linux服务的分类:   Linux服务: 1.      RPM包默认安装的服务 a)        独立的服务...
  • H002399
  • H002399
  • 2015年08月28日 10:16
  • 378
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android上下文服务管理查询过程
举报原因:
原因补充:

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