Open telephony packages:
frameworks/base/telephony/java/android/telephony
TelephonyManager provides a way for 3rd application interacting with internal telephony packages.
Class Overview (from android sdk document)
Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.
You do not instantiate this class directly; instead, you retrieve a reference to an instance throughContext.getSystemService(Context.TELEPHONY_SERVICE).
Note that access to some telephony information is permission-protected. Your application cannot access the protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the the methods through which you access the protected information.
第一条主线,以TelephonyManager为轴的上半部分,PhoneApp是一个非常特殊的app,由它通过调用PhoneFactory'创建一个Telephony(Internal),同时给第三方application暴露接口,供其通过TelephonyManager调用,TelephonyManager通过ITelephony(android的AIDL机制)获得PhoneInterfaceManager的远程对象,然后第三方application通过这个远程对象来调用service里的方法,这里的调用是两个独立进程之间进行的(如果不清楚aidl,可以假想为3rd application直接调用了PhoneInterfaceManager里的方法,但实际上是需要通过TelephonyManager才能调用的)
第二条主线,以TelephonyManager为轴的右半部分,TelephonyRegistry,顾名思义,是什么东西的注册,这种注册的一般都是为了实现回调功能,这个类的主要作用就是把第三方App里new 的PhoneStateListener和需要监听的动作通过调用TelephonyManager注册到TelephonyRegistry,当底层状态变化通知GSMPhone时,GSMPhone里的DefaultPhoneNotifier会通知TelephonyRegistry,然后注册到TelephonyRegistry的PhoneStateListener监听的事件会被触发(此时TelephonyRegistry还会发个boardCast),然后回调上层APP复写的方法。
(要理清关系的最好办法就是结合图阅读源码,那样会有更深的理解)
如果上面的机制看不懂也没关系,作为SDK开发只要知道以下部分就行,
If we want to track telephony state in 3rd application:
Take Settings as example to show how to achieve these.
packages/apps/Settings/src/com/android/settings/deviceinfo/ Status.java
111 private TelephonyManager mTelephonyManager;
169 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
170 @Override
171 public void onDataConnectionStateChanged(int state) {
UpdateDataState(); //you can override with your own code here
178 protected void onCreate(Bundle icicle) {
184 mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
268 protected void onResume() {
278 mTelephonyManager.listen(mPhoneStateListener,
279 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
337 private void updateDataState() {
338 int state = mTelephonyManager.getDataState();
341 switch (state) {
部分内容参考了http://wenku.baidu.com/view/49699c4f2e3f5727a5e96266.html