本文紧接着上一篇文章《Android Telephony分析(四) —- TelephonyManager详解 》的1.4小节。
从TelephonyRegistry的大部分方法中:
可以看出TelephonyRegistry主要的功能是上报消息,有两种方式:
1. 通过notifyXXX方法。
2. 通过发送broadcast。
至于发广播没什么好说的了,我们看看notifyXXX方法吧,以notifyCallStateForPhoneId()方法为例
public void notifyCallStateForPhoneId(int phoneId, int subId, int state,
String incomingNumber) {
if (!checkNotifyPermission("notifyCallState()")) {
return;
}
synchronized (mRecords) {
int phoneId = SubscriptionManager.getPhoneId(subId);
if (validatePhoneId(phoneId)) {
mCallState[phoneId] = state;
mCallIncomingNumber[phoneId] = incomingNumber;
for (Record r : mRecords) {
if (r.matchPhoneStateListenerEvent(PhoneStateListener.LISTEN_CALL_STATE) &&
(r.subId == subId) &&
(r.subId != SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)) {
try {
String incomingNumberOrEmpty = getCallIncomingNumber(r, phoneId);
r.callback.onCallStateChanged(state, incomingNumberOrEmpty);
} catch (RemoteException ex) {
mRemoveList.add(r.binder);
}
}
}
}
handleRemoveListLocked();
}
broadcastCallStateChanged(state, incomingNumber, phoneId, subId);
}
- 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集合
private final ArrayList<Record> mRecords = new ArrayList<Record>();
而Record是TelephonyRegistry中的内部类,TelephonyRegistry会把监听者的信息封装成一个Record对象,并且放进mRecords列表中管理。
class TelephonyRegistry extends ITelephonyRegistry.Stub {
private static class Record {
String callingPackage;
IBinder binder;
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) {
Record r;
find_and_add: {
IBinder b = callback.asBinder();
for (int i = 0; i < N; i++) {
r = mRecords.get(i);
if (b == r.binder) {
break find_and_add;
}
}
r = new Record();
r.binder = b;
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最常用的客户端,它继承自
public class DefaultPhoneNotifier implements PhoneNotifier {
在它构造方法中:
protected DefaultPhoneNotifier() {
mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
"telephony.registry"));
}
所以DefaultPhoneNotifier中大部分方法都依靠TelephonyRegistry对象来实现。
2 . 如何知道我需要监听的事件以及对应要重写的方法?
所有的可以监听的事件都定义在PhoneStateListener.java (frameworks\base\telephony\java\Android\telephony)中,
需要重写的方法,初始都定义在IPhoneStateListener.aidl(frameworks/base/telephony/java/com/android/internal/telephony)中
接着由PhoneStateListener初步重写了IPhoneStateListener.aidl接口中的方法:
/**
* The callback methods need to be called on the handler thread where
* this object was created. If the binder did that for us it'd be nice.
*/
IPhoneStateListener callback = new IPhoneStateListener.Stub() {
public void onServiceStateChanged(ServiceState serviceState) {
Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget();
}
...
}
在这里就可以找到某个事件对应的方法是什么。
当然可以自己新增事件以及在IPhoneStateListener.aidl中新增对应的接口,再实现该接口就可以了。
原文地址: http://blog.csdn.net/linyongan/article/details/52126969