Android(安卓)Telephony 分析[PART IV]
Open telephony packages:
frameworks/base/telephony/java/android/telephony
TelephonyManagerprovides a way for 3rd applicationinteracting with internal telephony packages.
Class Overview (from androidsdkdocument)
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 ispermission-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 thethemethods through which you access the protected information.
这个 图 我 想 详细 的 说 一下, 这个 图 演示 了 第三方 应 用, P hone S ervic e 及T elephony 的 关系。
第一条主线,以TelephonyManager为轴的上半部分,PhoneApp是一个非常特殊的app,由它通过调用PhoneFactory'创建一个Telephony(Internal),同时给第三方application暴露接口,供其通过TelephonyManager调用,TelephonyManager通过ITelephony(android的AIDL机制)获得PhoneInterfaceManager的远程对象,然后第三方application通过这个远程对象来调用service里的方法,这里的调用是两个独立进程之间进行的(如果不清楚aidl,可以假想为3rdapplication直接调用了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 3rdapplication:
1We should retrive an instance of TelephonyManager and get Phone service. //needed 2 Implement a PhoneStateListener what you want to listen. //not needed 3 You can also register a BroadCastReceiver to receive broadcast if you want. //not needed 4 Don’t forget add permission in manifest file. //neededTake Settings as example to show how to achieve these.
packages/apps/Settings/src/com/android/settings/deviceinfo/ Status.java
111private TelephonyManager mTelephonyManager;
169private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
170@Override
171public voidonDataConnectionStateChanged(int state) {
UpdateDataState();//you can override with your own code here
178protected void onCreate(Bundle icicle) {
184mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
268protected void onResume() {
278mTelephonyManager.listen(mPhoneStateListener,
279PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
337private void updateDataState() {
338int state = mTelephonyManager.getDataState();
341switch (state) {
部分内容参考了http://wenku.baidu.com/view/49699c4f2e3f5727a5e96266.html
更多相关文章
- java.lang.NullPointerException Attempt to invoke virtual met
- Android(安卓)后台线程调用前台线程的几种方法
- android生命周期研究
- android使用retrofit
- Android集成okhttp
- Android(安卓)调用Gmail发送邮件
- android
- Android布局优化:ViewStub标签实现延迟加载(源码解析原理)
- Android(安卓)创建线程执行任务