TelephonyManager

TelephonyManager作为Telephony相关业务的管理者,其为上层应用提供相关的Telephony服务,如获得sim卡信息,获得当前网络状态等等,但 TelephonyManager本身并不提供相关的服务,见如下。

可以从SDK来查找TelephonyManager具体提供了什么样的服务

TelephonyManager的代码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的时候,就会先启动PhoneApp
onCreate(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里就初始化了t elephony 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);
    }

参考

http://blog.csdn.net/qinjuning/article/details/7310620

http://blog.csdn.net/geekster/article/details/10038273



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值