TelephonyManager tm =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
我们经常在code中看到上面这段代码,但它的来源是在哪里?用途是是什么?
Code:
framework/base/core/java/android/app/SystemServiceRegistry.java
/**
* Manages all of the system services that can be returned by {@linkContext#getSystemService}.
* Used by {@link ContextImpl}.
*/
注册SystemService服务
registerService(Context.TELEPHONY_SERVICE,TelephonyManager.class,
newCachedServiceFetcher<TelephonyManager>() {
@Override
public TelephonyManagercreateService(ContextImpl ctx) {
return newTelephonyManager(ctx.getOuterContext());
}});
经过这样的注册,其他进程就可以通过Context对象的getSystemService()方法来获取其服务,例如:
TelephonyManager tm = (TelephonyManager) getSystemService(
Context.TELEPHONY_SERVICE);
或
/**{@hide} */
public static TelephonyManager from(Context context) {
return (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
所谓"phone"服务一直以来有两个;
TeleService 的PhoneInterfaceManager也是一个service,但它的注册方式和获取方式与 TelephonyManager完全不同
注册方式:
private void publish() {
if (DBG) log("publish: " + this);
ServiceManager.addService("phone", this);
}
获取方式:
final ITelephony iTel = ITelephony.Stub.asInterface(
ServiceManager.getService(Context.TELEPHONY_SERVICE));
PhoneInterfaceManager的继承关系:
PhoneInterfaceManager继承于ITelephony.Stub,ITelephony是一个aidl文件,文件位于framework下(framework\base\telephony\java\com\android\internal\telephony\ITelephony.aidl)
TelePhonyManager的继承关系:TelePhonyManager是一个独立的类,没有继承,位于framework.
但在TelephonyManager内部,获取到了三个Service的客户端,其中构造函数中获取了TelephonyRegistry的服务:
public TelephonyManager(Context context) {
ContextappContext = context.getApplicationContext();
if(appContext != null) {
mContext = appContext;
} else{
mContext = context;
}
if(sRegistry == null) {
//获取TelephonyRegistry的服务
sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));
}
}
然后通过getSubscriberInfo()获取了PhoneSubInfoProxy的服务:
private IPhoneSubInfo getSubscriberInfo() {
//获取的是PhoneSubInfoProxy的服务
return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
}
以及通过getITelephony()获取了PhoneInterfaceManager的服务:
private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
}