av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

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

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

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

什么是Telecom服務(wù)

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

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

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

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ù)是如何啟動(dòng)與初始化的。

Telecom進(jìn)程的啟動(dòng)與初始化

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

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

調(diào)用系統(tǒng)服務(wù)管家SystemServiceManager的接口startService創(chuàng)建新的服務(wù),并注冊(cè)到系統(tǒng)中,最后調(diào)用onStart()啟動(dòng)服務(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ù)對(duì)象(服務(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)用,撥號(hào)應(yīng)用以及SIM通話管理應(yīng)用(不知道這個(gè)什么鬼)告知PackageManagerService(PMS),以便在適當(dāng)?shù)臅r(shí)候可以找到應(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ù)并沒啟動(dòng),那么究竟Telecom服務(wù)在哪里啟動(dòng)的了?仔細(xì)看TelecomLoaderService的源代碼,其中有一個(gè)onBootPhase的函數(shù),用于SystemServer告知系統(tǒng)服務(wù)目前系統(tǒng)啟動(dòng)所處的階段。這里可以看到,等(ActivityManagerService)AMS啟動(dòng)完成以后,就可以開始連接Telecom服務(wù)了:

首先,注冊(cè)默認(rèn)應(yīng)用(SMS/Dialer etc)通知對(duì)象,以便這些應(yīng)用發(fā)送變更(如下載了一個(gè)第三方的SMS應(yīng)用時(shí),可以通知系統(tǒng)這一變化); 接著,注冊(cè)運(yùn)營(yíng)商配置變化的廣播接收器,如果配置有變化時(shí),系統(tǒng)會(huì)收到通知; 綁定TelecomService,并將其注冊(cè)到系統(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)啟動(dò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ù)連接中斷時(shí),則重新連接:

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ù)時(shí),調(diào)用TelecomService的onBind接口,對(duì)整個(gè)Telecom系統(tǒng)進(jìn)行初始化,并返回一個(gè)IBinder接口:

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

Telecom系統(tǒng)初始化,主要工作是新建一個(gè)TelecomSystem的類,在這個(gè)類中,會(huì)對(duì)整個(gè)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); // 新建一個(gè)單例模式的對(duì)象 TelecomSystem.setInstance(new TelecomSystem(....)); } .... } }

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

public TelecomSystem( Context context, /* 用戶未接來電通知類(不包括已接或者拒絕的電話) */ MissedCallNotifierImplFactory missedCallNotifierImplFactory, /* 查詢來電信息 */ CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, /* 耳機(jī)接入狀態(tài)監(jiān)聽 */ HeadsetMediaButtonFactory headsetMediaButtonFactory, /* 距離傳感器管理 */ ProximitySensorManagerFactory proximitySensorManagerFactory, /* 通話時(shí)電話管理 */ InCallWakeLockControllerFactory inCallWakeLockControllerFactory, /* 音頻服務(wù)管理 */ AudioServiceFactory audioServiceFactory, /* 藍(lán)牙設(shè)備管理 */ BluetoothPhoneServiceImplFactory bluetoothPhoneServiceImplFactory, BluetoothVoIPServiceImplFactory bluetoothVoIPServiceImplFactory, /* 查詢所有超時(shí)信息 */ Timeouts.Adapter timeoutsAdapter, /* 響鈴播放 */ AsyncRingtonePlayer asyncRingtonePlayer, /* 電話號(hào)碼幫助類 */ PhoneNumberUtilsAdapter phoneNumberUtilsAdapter, /* 通話時(shí)阻斷通知 */ InterruptionFilterProxy interruptionFilterProxy) { mContext = context.getApplicationContext(); // 初始化telecom相關(guān)的feature TelecomFeature.makeFeature(mContext); // 初始化telecom的數(shù)據(jù)庫 TelecomSystemDB.initialize(mContext); // 創(chuàng)建一個(gè)PhoneAccount注冊(cè)管理類 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); // 注冊(cè)需要接收的廣播 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)建一個(gè)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到底起到個(gè)什么作用了?按照源碼中的說明來理解,PhoneAccount表示了不同的接聽或者撥打電話的方式,比如用戶可以通過SIM卡來撥打電話,也可以撥打視頻電話,抑或一個(gè)緊急通話,甚至可以通過telephony內(nèi)部的接口來實(shí)現(xiàn)撥號(hào),而Android正是通過PhoneAccount來區(qū)分這幾種通話方式的。與之相對(duì)應(yīng)的一個(gè)類PhoneAccountHandle則是用于表示哪一個(gè)用戶正在使用通話服務(wù)。

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

到此這篇關(guān)于Android 系統(tǒng)服務(wù)TelecomService啟動(dòng)過程原理分析的文章就介紹到這了,更多相關(guān)Android 系統(tǒng)服務(wù)TelecomService啟動(dòng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)! $.get('https://blog.csdn.net/wang2119/article/uvc/58164251');
標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 亚洲日本久久 | 极品淫少妇 | 国产视频在线观看视频 | 欧美国产在线视频 | 中文字幕一二三四区 | 亚洲精品日韩丝袜精品 | 91在线精品视频 | 911亚洲精品| 亚洲精品在线视频观看 | 天天摸天天爽 | 午夜看片 | 91人人爽| 成人精品在线观看 | 欧美日韩大片 | 日韩资源 | 国产精品一区二区不卡 | 国产精品久久久久久久久久久久久久久 | 欧美在线性爱视频 | 亚洲天堂一区 | 日日干夜夜爽 | 日本一区二区三区免费观看 | 欧美精品一区在线 | 免费在线看a | 91成人免费版 | 亚洲字幕 | 不卡的av| 风间由美一区二区三区 | 国产91精品在线观看 | 玖草视频 | av网站在线免费观看 | 日本精品久久 | 国产极品国产极品 | 9.1成人免费看片 | 国产午夜影院 | 精品国产999久久久免费 | 国产精品美女久久久 | 一级黄色小视频 | 91亚洲国产成人久久精品网站 | 天天干夜夜操 | 美女在线播放 | 国产精品网站在线观看 |