<application android:name="PhoneApp" android:persistent="true" android:label="@string/phoneAppLabel" android:icon="@mipmap/ic_launcher_phone" android:supportsRtl="true">这个声明创建了一个名叫“PhoneApp”的application,并且确定了他的name、label、icon等信息,而且将该application的persistent属性置为true。那么这个persistent属性的作用是什么呢?
@PhoneApp.java public void onCreate() { if (UserHandle.myUserId() == 0) { mPhoneGlobals = new PhoneGlobals(this); mPhoneGlobals.onCreate(); } }接下来我们来看PhoneGlobals的onCreate过程:
@PhoneGlobals public void onCreate() { if (phone == null) { //创建Phone对象 PhoneFactory.makeDefaultPhones(this); phone = PhoneFactory.getDefaultPhone(); //初始化CallManager mCM = CallManager.getInstance(); mCM.registerPhone(phone); //初始化NotificationMgr,用于状态栏通知 notificationMgr = NotificationMgr.init(this); //初始化CallController callController = CallController.init(this, callLogger, callGatewayManager); //得到铃声控制 ringer = Ringer.init(this, bluetoothManager); audioRouter = new AudioRouter(this, bluetoothManager, wiredHeadsetManager, mCM); //初始化callCommandService callCommandService = new CallCommandService(this, mCM, callModeler, dtmfTonePlayer, audioRouter); //初始化PhoneInterfaceManager phoneMgr = PhoneInterfaceManager.init(this, phone, callHandlerServiceProxy); //初始化CallNotifer,响铃等动作在这里面完成 notifier = CallNotifier.init(this, phone, ringer, callLogger, callStateMonitor, bluetoothManager, callModeler); //注册SIM卡状态监听 IccCard sim = phone.getIccCard(); if (sim != null) { sim.registerForNetworkLocked(mHandler, EVENT_SIM_NETWORK_LOCKED, null); } } }从以上代码可以看出,PhoneGlobals的初始化过程中要先通过PhoneFactory的makeDefaultPhones()方法创建Phone对象,接着完成了一系列与Telephony相关的重要服务的初始化,比如CallManager、NotificationMgr、CallCommandService、PhoneInterfaceManager、CallNotifier等。
@PhoneFactory.java public static void makeDefaultPhones(Context context) { makeDefaultPhone(context); } public static void makeDefaultPhone(Context context) { synchronized(Phone.class) { if (!sMadeDefaults) { //创建DefaultPhoneNotifier,负责通知Phone的状态 sPhoneNotifier = new DefaultPhoneNotifier(); //得到当前的网络类型 int networkMode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.PREFERRED_NETWORK_MODE, preferredNetworkMode); //根据当前网络类型来创建RILJ,负责Framework与RIL层交互 sCommandsInterface = new RIL(context, networkMode, cdmaSubscription); //创建UiccController并间接创建UiccCard、UiccCardApplication、IccFileHandler、IccRecords、CatService等服务 UiccController.make(context, sCommandsInterface); //根据当前的Phone类型创建不同的PhoneProxy int phoneType = TelephonyManager.getPhoneType(networkMode); if (phoneType == PhoneConstants.PHONE_TYPE_GSM) { //GSMPhone sProxyPhone = new PhoneProxy(new GSMPhone(context, sCommandsInterface, sPhoneNotifier)); } else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) { switch (TelephonyManager.getLteOnCdmaModeStatic()) { case PhoneConstants.LTE_ON_CDMA_TRUE: //CDMALTEPhone sProxyPhone = new PhoneProxy(new CDMALTEPhone(context, sCommandsInterface, sPhoneNotifier)); break; case PhoneConstants.LTE_ON_CDMA_FALSE: default: //CDMAPhone sProxyPhone = new PhoneProxy(new CDMAPhone(context, sCommandsInterface, sPhoneNotifier)); break; } } } } }经过上面两段代码,我们看到了Phone对象的创建过程:
4、再用3中拿到的Phone创建PhoneProxy对象,这个代理对象才是可以直接使用的Phone对象;
这个思想贯穿了整个Android的通讯机制,无论是RIL层、还是Telephony中,都能得到体现。