Android Telephony分析(五) ---- TelephonyRegistry详解
 本文緊接著上一篇文章《Android Telephony分析(四) —- TelephonyManager詳解 》的1.4小節。?
 從TelephonyRegistry的大部分方法中:?
 ?
 可以看出TelephonyRegistry主要的功能是上報消息,有兩種方式:?
 1. 通過notifyXXX方法。?
 2. 通過發送broadcast。?
 至于發廣播沒什么好說的了,我們看看notifyXXX方法吧,以notifyCallStateForPhoneId()方法為例
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
 通過初步分析notifyCallState()方法,你會發現有兩個疑問:?
 1. mRecords是什么??
 2. Record.callback又是什么??
 好了,帶著上面兩種疑問,我們繼續分析代碼吧。?
 接著你會發現mRecords是由Record對象組成的List集合
- 1
- 1
而Record是TelephonyRegistry中的內部類,TelephonyRegistry會把監聽者的信息封裝成一個Record對象,并且放進mRecords列表中管理。
class TelephonyRegistry extends ITelephonyRegistry.Stub {private static class Record {String callingPackage;IBinder binder;//Record.callback是實現了IPhoneStateListener接口的類,//也就是繼承了或者重寫了PhoneStateListener的類IPhoneStateListener callback;IOnSubscriptionsChangedListener onSubscriptionsChangedListenerCallback;int callerUserId;//存儲用來記錄監聽的事件int events;int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;int phoneId = SubscriptionManager.INVALID_PHONE_INDEX;boolean canReadPhoneState;boolean matchPhoneStateListenerEvent(int events) {return (callback != null) && ((events & this.events) != 0);}boolean matchOnSubscriptionsChangedListener() {return (onSubscriptionsChangedListenerCallback != null);}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
在TelephonyRegistry.java的listen方法中,管理并維護著mRecords列表:
public void listenForSubscriber(int subId, String pkgForDebug, IPhoneStateListener callback,int events, boolean notifyNow) {listen(pkgForDebug, callback, events, notifyNow, subId);}private void listen(String callingPackage, IPhoneStateListener callback, int events,boolean notifyNow, int subId) {...synchronized (mRecords) {// registerRecord r;find_and_add: {//得到監聽者的信息IBinder b = callback.asBinder();//遍歷mRecords列表for (int i = 0; i < N; i++) {r = mRecords.get(i);if (b == r.binder) {//退出循環break find_and_add;}}//如果監聽者還沒有被加到mRecords列表中r = new Record();r.binder = b;//新建一個Record對象并且加入mRecords列表中mRecords.add(r);if (DBG) log("listen: add new record");}//存儲監聽者r.callback = callback;...//存儲監聽者所監聽的事件r.events = events;//如果需要現在發通知的話if (notifyNow && validatePhoneId(phoneId)) {if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {try {//注冊之后,馬上通知一次監聽者r.callback.onServiceStateChanged(new ServiceState(mServiceState[phoneId]));} catch (RemoteException ex) {remove(r.binder);}}...}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
本文來自http://blog.csdn.net/linyongan?,轉載請務必注明出處。
 常見的注冊監聽和發通知的流程,以監聽Call狀態變化為例:?
 
 總體看分成3步:?
 1. 根據需要監聽的事件,重寫PhoneStateListener中對應的方法,如果需要監聽LISTEN_CALL_STATE,那么需要重寫 onCallStateChanged()方法;如果需要監聽LISTEN_SERVICE_STATE,則需要重寫onServiceStateChanged()方法。(步驟1)?
 2. 調用TelephonyManager的listen()方法,傳遞PhoneStateListener對象和events。到這里監聽的操作一直執行到步驟4就結束了。?
 3. 當有Call狀態變化消息上來時,通知上報的路徑:?
 RILJ—>CallTracker—>Phone—>DefaultPhoneNotifier—>TelephonyRegistry—>監聽者?
 最后也是來到監聽者重寫的PhoneStateListener的那個方法中,整個過程也是一個回調。
 其他需要說明的是:?
 1. DefaultPhoneNotifier是TelephonyRegistry最常用的客戶端,它繼承自
- 1
- 1
在它構造方法中:
protected DefaultPhoneNotifier() {//通過ServiceManager得到了TelephonyRegistry的代理對象mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));}- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
所以DefaultPhoneNotifier中大部分方法都依靠TelephonyRegistry對象來實現。
 2 . 如何知道我需要監聽的事件以及對應要重寫的方法??
 所有的可以監聽的事件都定義在PhoneStateListener.java (frameworks\base\telephony\java\Android\telephony)中,?
 ?
 需要重寫的方法,初始都定義在IPhoneStateListener.aidl(frameworks/base/telephony/java/com/android/internal/telephony)中?
 ?
 接著由PhoneStateListener初步重寫了IPhoneStateListener.aidl接口中的方法:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
 在這里就可以找到某個事件對應的方法是什么。?
 
當然可以自己新增事件以及在IPhoneStateListener.aidl中新增對應的接口,再實現該接口就可以了。
 
原文地址: http://blog.csdn.net/linyongan/article/details/52126969
總結
以上是生活随笔為你收集整理的Android Telephony分析(五) ---- TelephonyRegistry详解的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Android Telephony分析(
- 下一篇: Android Telephony分析(
