电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android 系統(tǒng)服務(wù)TelecomService啟動過程原理分析

瀏覽:2日期:2022-09-23 11:03:55

由于一直負(fù)責(zé)的是Android Telephony部分的開發(fā)工作,對于通信過程的上層部分Telecom服務(wù)以及UI都沒有認(rèn)真研究過。最近恰好碰到一個通話方面的問題,涉及到了Telecom部分,因而就花時間仔細(xì)研究了下相關(guān)的代碼。這里做一個簡單的總結(jié)。這篇文章,主要以下兩個部分的內(nèi)容:

什么是Telecom服務(wù)?其作用是什么? Telecom模塊的啟動與初始化過程;

接下來一篇文章,主要以實際通話過程為例,分析下telephony收到來電后如何將電話信息發(fā)送到Telecom模塊以及Telecom是如何處理來電。

什么是Telecom服務(wù)

Telecom是Android的一個系統(tǒng)服務(wù),其主要作用是管理Android系統(tǒng)當(dāng)前的通話,如來電顯示,接聽電話,掛斷電話等功能,在Telephony模塊與上層UI之間起到了一個橋梁的作用。比如,Telephony有接收到新的來電時,首先會告知Telecom,然后由Telecom服務(wù)通知上層應(yīng)用來電信息,并顯示來電界面。

Telecom服務(wù)對外提供了一個接口類TelecomManager,通過其提供的接口,客戶端可以查詢通話狀態(tài),發(fā)送通話請求以及添加通話鏈接等。

從Telecom進程對應(yīng)的AndroidManifest.xml文件來看,Telecom進程的用戶ID跟系統(tǒng)進程用戶ID相同,是系統(tǒng)的核心服務(wù)。那么,其中android:process='system'這個屬性值表示什么意思了?查看官方文檔,這個表示Telecom將啟動在進程system中,這樣可以跟其他進程進行資源共享了(對于Android這個全局進程,就是SystemServer所在的進程)。

android:process

By setting this attribute to a process name that’s shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.

If the name assigned to this attribute begins with a colon (‘:’), a new process, private to the application, is created when it’s needed. If the process name begins with a lowercase character, a global process of that name is created. A global process can be shared with other applications, reducing resource usage.

<manifest xmlns:android='http://schemas.android.com/apk/res/android' xmlns:androidprv='http://schemas.android.com/apk/prv/res/android' package='com.android.server.telecom' android:versionCode='1' android:versionName='1.0.0' coreApp='true' android:sharedUserId='android.uid.system'> <application android:label='@string/telecommAppLabel' android:icon='@mipmap/ic_launcher_phone' android:allowBackup='false' android:supportsRtl='true' android:process='system' android:usesCleartextTraffic='false' android:defaultToDeviceProtectedStorage='true' android:directBootAware='true'> .... // 包含TelecomService <service android:name='.components.TelecomService' android:singleUser='true' android:process='system'> <intent-filter> <action android:name='android.telecom.ITelecomService' /> </intent-filter> </service> .... </application> </manifest>

代碼路徑:

/android/applications/sources/services/Telecomm//android/frameworks/base/telecomm/

了解了什么是Telecom服務(wù)之后,就來看一看Telecom服務(wù)是如何啟動與初始化的。

Telecom進程的啟動與初始化

在SystemServer進程初始化完成啟動完系統(tǒng)的核心服務(wù)如ActivityManagerService后,就會加載系統(tǒng)其它服務(wù),這其中就包含了一個與Telecom服務(wù)啟動相關(guān)的系統(tǒng)服務(wù)專門用于加載Telecom:

private void startOtherServices() { .... //啟動TelecomLoaderService系統(tǒng)服務(wù),用于加載Telecom mSystemServiceManager.startService(TelecomLoaderService.class); // 啟動telephony注冊服務(wù),用于注冊監(jiān)聽telephony狀態(tài)的接口 telephonyRegistry = new TelephonyRegistry(context); ServiceManager.addService('telephony.registry', telephonyRegistry); }

調(diào)用系統(tǒng)服務(wù)管家SystemServiceManager的接口startService創(chuàng)建新的服務(wù),并注冊到系統(tǒng)中,最后調(diào)用onStart()啟動服務(wù)。

public class SystemServiceManager { @SuppressWarnings('unchecked') public SystemService startService(String className) { final Class<SystemService> serviceClass; try { serviceClass = (Class<SystemService>)Class.forName(className); } catch (ClassNotFoundException ex) { .... } return startService(serviceClass); } // 服務(wù)的class文件來創(chuàng)建新的服務(wù)對象(服務(wù)必須繼承SystemService) @SuppressWarnings('unchecked') public <T extends SystemService> T startService(Class<T> serviceClass) { try { final String name = serviceClass.getName(); Slog.i(TAG, 'Starting ' + name); Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, 'StartService ' + name); // Create the service. if (!SystemService.class.isAssignableFrom(serviceClass)) { throw new RuntimeException('Failed to create ' + name + ': service must extend ' + SystemService.class.getName()); } final T service; try { Constructor<T> constructor = serviceClass.getConstructor(Context.class); service = constructor.newInstance(mContext); } catch (InstantiationException ex) { throw new RuntimeException('Failed to create service ' + name + ': service could not be instantiated', ex); } .... // Register it. mServices.add(service); // Start it. try { service.onStart(); } catch (RuntimeException ex) { throw new RuntimeException('Failed to start service ' + name + ': onStart threw an exception', ex); } return service; } finally { Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); } } }

創(chuàng)建TelecomLoaderService系統(tǒng)服務(wù),將系統(tǒng)默認(rèn)的SMS應(yīng)用,撥號應(yīng)用以及SIM通話管理應(yīng)用(不知道這個什么鬼)告知PackageManagerService(PMS),以便在適當(dāng)?shù)臅r候可以找到應(yīng)用。

public class TelecomLoaderService extends SystemService { ... public TelecomLoaderService(Context context) { super(context); mContext = context; registerDefaultAppProviders(); } @Override public void onStart() { } private void registerDefaultAppProviders() { final PackageManagerInternal packageManagerInternal = LocalServices.getService( PackageManagerInternal.class); // Set a callback for the package manager to query the default sms app. packageManagerInternal.setSmsAppPackagesProvider( new PackageManagerInternal.PackagesProvider() { @Override public String[] getPackages(int userId) { synchronized (mLock) { .... ComponentName smsComponent = SmsApplication.getDefaultSmsApplication( mContext, true); if (smsComponent != null) { return new String[]{smsComponent.getPackageName()}; } return null; } }); // Set a callback for the package manager to query the default dialer app. packageManagerInternal.setDialerAppPackagesProvider( new PackageManagerInternal.PackagesProvider() { @Override public String[] getPackages(int userId) { synchronized (mLock) { .... String packageName = DefaultDialerManager.getDefaultDialerApplication(mContext); if (packageName != null) { return new String[]{packageName}; } return null; } }); // Set a callback for the package manager to query the default sim call manager. packageManagerInternal.setSimCallManagerPackagesProvider( new PackageManagerInternal.PackagesProvider() { @Override public String[] getPackages(int userId) { synchronized (mLock) { .... TelecomManager telecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(userId); if (phoneAccount != null) { return new String[]{phoneAccount.getComponentName().getPackageName()}; } return null; } }); } }

到目前,好像Telecom服務(wù)并沒啟動,那么究竟Telecom服務(wù)在哪里啟動的了?仔細(xì)看TelecomLoaderService的源代碼,其中有一個onBootPhase的函數(shù),用于SystemServer告知系統(tǒng)服務(wù)目前系統(tǒng)啟動所處的階段。這里可以看到,等(ActivityManagerService)AMS啟動完成以后,就可以開始連接Telecom服務(wù)了:

首先,注冊默認(rèn)應(yīng)用(SMS/Dialer etc)通知對象,以便這些應(yīng)用發(fā)送變更(如下載了一個第三方的SMS應(yīng)用時,可以通知系統(tǒng)這一變化); 接著,注冊運營商配置變化的廣播接收器,如果配置有變化時,系統(tǒng)會收到通知; 綁定TelecomService,并將其注冊到系統(tǒng)中。

public class TelecomLoaderService extends SystemService { private static final ComponentName SERVICE_COMPONENT = new ComponentName( 'com.android.server.telecom', 'com.android.server.telecom.components.TelecomService'); private static final String SERVICE_ACTION = 'com.android.ITelecomService'; // 當(dāng)前系統(tǒng)啟動的階段 @Override public void onBootPhase(int phase) { if (phase == PHASE_ACTIVITY_MANAGER_READY) { registerDefaultAppNotifier(); registerCarrierConfigChangedReceiver(); connectToTelecom(); } } //綁定Telecom服務(wù) private void connectToTelecom() { synchronized (mLock) { if (mServiceConnection != null) { // TODO: Is unbinding worth doing or wait for system to rebind? mContext.unbindService(mServiceConnection); mServiceConnection = null; } TelecomServiceConnection serviceConnection = new TelecomServiceConnection(); Intent intent = new Intent(SERVICE_ACTION); intent.setComponent(SERVICE_COMPONENT); int flags = Context.BIND_IMPORTANT | Context.BIND_FOREGROUND_SERVICE | Context.BIND_AUTO_CREATE; // Bind to Telecom and register the service if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.SYSTEM)) { mServiceConnection = serviceConnection; } } } }

服務(wù)綁定:https://developer.android.com/guide/components/bound-services.html

將服務(wù)添加到ServiceManager中,如果Telecom服務(wù)連接中斷時,則重新連接:

public class TelecomLoaderService extends SystemService { private class TelecomServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { // Normally, we would listen for death here, but since telecom runs in the same process // as this loader (process='system') thats redundant here. try { service.linkToDeath(new IBinder.DeathRecipient() { @Override public void binderDied() {connectToTelecom(); } }, 0); SmsApplication.getDefaultMmsApplication(mContext, false); //添加Telecom服務(wù) ServiceManager.addService(Context.TELECOM_SERVICE, service); .... } @Override public void onServiceDisconnected(ComponentName name) { connectToTelecom(); } } }

綁定服務(wù)時,調(diào)用TelecomService的onBind接口,對整個Telecom系統(tǒng)進行初始化,并返回一個IBinder接口:

/** * Implementation of the ITelecom interface. */ public class TelecomService extends Service implements TelecomSystem.Component { @Override public IBinder onBind(Intent intent) { // 初始化整個Telecom系統(tǒng) initializeTelecomSystem(this); //返回IBinder接口 synchronized (getTelecomSystem().getLock()) { return getTelecomSystem().getTelecomServiceImpl().getBinder(); } } }

Telecom系統(tǒng)初始化,主要工作是新建一個TelecomSystem的類,在這個類中,會對整個Telecom服務(wù)的相關(guān)類都初始化:

static void initializeTelecomSystem(Context context) { if (TelecomSystem.getInstance() == null) { final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // 用于獲取聯(lián)系人 contactInfoHelper = new ContactInfoHelper(context); // 新建一個單例模式的對象 TelecomSystem.setInstance(new TelecomSystem(....)); } .... } }

構(gòu)造一個單例TelecomSystem對象:

public TelecomSystem( Context context, /* 用戶未接來電通知類(不包括已接或者拒絕的電話) */ MissedCallNotifierImplFactory missedCallNotifierImplFactory, /* 查詢來電信息 */ CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, /* 耳機接入狀態(tài)監(jiān)聽 */ HeadsetMediaButtonFactory headsetMediaButtonFactory, /* 距離傳感器管理 */ ProximitySensorManagerFactory proximitySensorManagerFactory, /* 通話時電話管理 */ InCallWakeLockControllerFactory inCallWakeLockControllerFactory, /* 音頻服務(wù)管理 */ AudioServiceFactory audioServiceFactory, /* 藍(lán)牙設(shè)備管理 */ BluetoothPhoneServiceImplFactory bluetoothPhoneServiceImplFactory, BluetoothVoIPServiceImplFactory bluetoothVoIPServiceImplFactory, /* 查詢所有超時信息 */ Timeouts.Adapter timeoutsAdapter, /* 響鈴播放 */ AsyncRingtonePlayer asyncRingtonePlayer, /* 電話號碼幫助類 */ PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, /* 通話時阻斷通知 */ InterruptionFilterProxy interruptionFilterProxy) { mContext = context.getApplicationContext(); // 初始化telecom相關(guān)的feature TelecomFeature.makeFeature(mContext); // 初始化telecom的數(shù)據(jù)庫 TelecomSystemDB.initialize(mContext); // 創(chuàng)建一個PhoneAccount注冊管理類 mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext); .... // 初始化通話管家,正是它負(fù)責(zé)與上層UI的交互 mCallsManager = new CallsManager( mContext, mLock, mContactsAsyncHelper, callerInfoAsyncQueryFactory, mMissedCallNotifier, mPhoneAccountRegistrar, headsetMediaButtonFactory, proximitySensorManagerFactory, inCallWakeLockControllerFactory, audioServiceFactory, bluetoothManager, wiredHeadsetManager, systemStateProvider, defaultDialerAdapter, timeoutsAdapter,AsyncRingtonePlayer, phoneNumberUtilsAdapter, interruptionFilterProxy); CallsManager.initialize(mCallsManager); // 注冊需要接收的廣播 mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER); mContext.registerReceiver(mUserStartingReceiver, USER_STARTING_FILTER); mContext.registerReceiver(mFeatureChangedReceiver, FEATURE_CHANGED_FILTER); mContext.registerReceiver(mEmergencyReceiver, EMERGENCY_STATE_CHANGED); .... // 所有來電與去電的處理中轉(zhuǎn)站 mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager); // 創(chuàng)建一個TelecomServiceImpl用于調(diào)用TelecomService的接口 mTelecomServiceImpl = new TelecomServiceImpl( mContext, mCallsManager, mPhoneAccountRegistrar, new CallIntentProcessor.AdapterImpl(), new UserCallIntentProcessorFactory() { @Override public UserCallIntentProcessor create(Context context, UserHandle userHandle) { return new UserCallIntentProcessor(context, userHandle); } }, defaultDialerAdapter, new TelecomServiceImpl.SubscriptionManagerAdapterImpl(), mLock); // 執(zhí)行特定的初始化操作 initialize(mContext); } }

Android Telephony中的PhoneAccount到底起到個什么作用了?按照源碼中的說明來理解,PhoneAccount表示了不同的接聽或者撥打電話的方式,比如用戶可以通過SIM卡來撥打電話,也可以撥打視頻電話,抑或一個緊急通話,甚至可以通過telephony內(nèi)部的接口來實現(xiàn)撥號,而Android正是通過PhoneAccount來區(qū)分這幾種通話方式的。與之相對應(yīng)的一個類PhoneAccountHandle則是用于表示哪一個用戶正在使用通話服務(wù)。

至此整個Telecom服務(wù)就啟動完成了,這樣Telecom服務(wù)就可以處理來電或者去電了。在接下來的一篇文章里,將分析下來電是如何在Telecom中傳遞與處理,然后發(fā)送到上層UI界面的。

到此這篇關(guān)于Android 系統(tǒng)服務(wù)TelecomService啟動過程原理分析的文章就介紹到這了,更多相關(guān)Android 系統(tǒng)服務(wù)TelecomService啟動內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)! $.get('https://blog.csdn.net/wang2119/article/uvc/58164251');
標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 气动隔膜阀_气动隔膜阀厂家_卫生级隔膜阀价格_浙江浙控阀门有限公司 | 电动葫芦|环链电动葫芦-北京凌鹰名优起重葫芦| 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 充气膜专家-气膜馆-PTFE膜结构-ETFE膜结构-商业街膜结构-奥克金鼎 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 农业仪器网 - 中国自动化农业仪器信息交流平台 | POS机办理_个人pos机免费领取-银联pos机申请首页 | 诺冠气动元件,诺冠电磁阀,海隆防爆阀,norgren气缸-山东锦隆自动化科技有限公司 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 健康管理师报考条件,考试时间,报名入口—首页 | 展厅设计-展馆设计-专业企业展厅展馆设计公司-昆明华文创意 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 信阳市建筑勘察设计研究院有限公司| 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 济南网站策划设计_自适应网站制作_H5企业网站搭建_济南外贸网站制作公司_锐尚 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 合同书格式和范文_合同书样本模板_电子版合同,找范文吧 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 苏州西朗门业-欧盟CE|莱茵UL双认证的快速卷帘门品牌厂家 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 泡沫消防车_水罐消防车_湖北江南专用特种汽车有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 电缆隧道在线监测-智慧配电站房-升压站在线监测-江苏久创电气科技有限公司 |