您的位置:首页 > 其它

TelephonyManager

2015-09-19 18:09 337 查看
TelephonyManager作为Telephony相关业务的管理者,其为上层应用提供相关的Telephony服务,如获得sim卡信息,获得当前网络状态等等,但 TelephonyManager本身并不提供相关的服务,见如下。可以从SDK来查找TelephonyManager具体提供了什么样的服务http://developer.android.com/reference/android/telephony/TelephonyManager.htmlTelephonyManager的代码frameworks/base/telephony/java/android/telephony/TelephonyManager.java从代码中可以看出,当上层或其它应用调用TelephonyManager服务时,TelephonyManager仅仅是将对应的请求发送给其它模块来获得具体的内容,如getDeviceId() 通过iphonesubinfo service来获得服务
public String getDeviceId() {
try {
return getSubscriberInfo().getDeviceId();
} catch (RemoteException ex) {
return null;
} catch (NullPointerException ex) {
return null;
}
}

private IPhoneSubInfo getSubscriberInfo() {
return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
}
iphonesubinfo service的注册
PhoneFactory->ProxyController->PhoneSubInfoController->ServiceManager.addService("iphonesubinfo", this);
如 getAllCellInfo()通过"phone" service来获得服务
public List<CellInfo> getAllCellInfo(long subId) {
try {
return getITelephony().getAllCellInfoUsingSubId(subId);
} catch (RemoteExceptionex) {
return null;
} catch (NullPointerExceptionex) {
return null;
}
}
private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
}
<application android:name="PhoneApp">在Launcher启动该App的时候,就会先启动PhoneApponCreate(PhoneApp) -> PhoneGlobals().onCreate() ->PhoneInterfaceManager.init()-> new PhoneInterfaceManager() -> publish (addService("phone"))如 getCallState()通过"telecom"来获得服务
public int getCallState() {
try {
return getTelecomService().getCallState();
} catch (RemoteException | NullPointerException e) {
return CALL_STATE_IDLE;
}
}
private ITelecomService getTelecomService() {
return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
}
mTelecomService = new TelecomServiceImpl(mMissedCallNotifier, mPhoneAccountRegistrar,
mCallsManager, this);
TelecomApp->onCreate()->ServiceManager.addService(Context.TELECOM_SERVICE, mTelecomService);
以上三个见如下流程图所示TelephonyManager的注册SystemServer-> createSystemContext() -> ContextImpl.createSystemContext()->
new ContextImpl()
在生成ContextImpl()对象时,会先初始化类的static成员变量,多个子类继承时,在生成实例时,也仅只会初始化static一次
private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =new HashMap<String, ServiceFetcher>();private static void register Service(StringserviceName, ServiceFetcherfetcher) {if (!(fetcher instanceof StaticServiceFetcher)) {fetcher.mContext CacheIndex = sNextPerContextServiceCacheIndex++;}SYSTEM_SERVICE_MAP.put(serviceName, fetcher);}static {registerService(TELEPHONY_SERVICE, newServiceFetcher() {public Object createService(ContextImplctx) {return new TelephonyManager(ctx.getOuterContext()); //生成TelephonyManager实例}});}public static final String TELEPHONY_SERVICE = "phone";
可以看出,,在systemserver里会创建系统上下文,即在systemserver里就初始化了telephony Service.获得TelephonyManager的实例
从上述实现可以看出,将TelephonyManager的实例保存到ContextImpl的SYSTEM_SERVICE_MAP中,因此对于应用程序要获得TelephonyManager的对象直接通过context的getSystemService(), 就行了
public Object getSystemService(Stringname) {Service Fetcherfetcher = SYSTEM_SERVICE_MAP.get(name);return fetcher == null ? null : fetcher.getService(this);}
上述获取TelephonyManager仅是针对第三方应用,而系统应用可以直接通过 TelephonyManager.getDefault()或TelephonyManager.from()获得
   /** @hide//hide表示应用程序无法直接使用,但是可以通过反射获得/* @deprecated - use getSystemService as described above */public static TelephonyManager getDefault() {return sInstance;}/** {@hide} */public static TelephonyManager from(Context context) {return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);}
参考/article/1390930.html/article/9926557.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: