Android Telephony 分析[PART IV]

来源:互联网 发布:mac os 网络恢复 编辑:程序博客网 时间:2024/05/22 16:55

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.


[原创]Android Telephony 分析[PART IV] - 〇〇柒 - 朝阳光出发

 这个详细一下,这个演示第三方用,Phone Service及Telephony关系。

第一条主线,TelephonyManager上半部分PhoneApp一个非常特殊app,通过调用PhoneFactory'创建一个Telephony(Internal),同时第三方application暴露接口,通过TelephonyManager调用,TelephonyManager通过ITelephonyandroid的AIDL机制)获得PhoneInterfaceManager远程对象然后第三方application通过这个远程对象调用service方法这里调用两个独立进程之间进行如果不清楚aidl可以3rd application直接调用了PhoneInterfaceManager方法,实际上需要通过TelephonyManager才能调用


第二条主线,TelephonyManager右半部分,TelephonyRegistry,顾名思义,什么东西注册,这种注册一般都是为了实现回调功能这个类主要作用就是第三方Appnew 的PhoneStateListener需要监听动作通过调用TelephonyManager注册到TelephonyRegistry,底层状态变化通知GSMPhone时,GSMPhone的DefaultPhoneNotifier通知TelephonyRegistry,然后注册到TelephonyRegistry的PhoneStateListener监听事件会被触发(此时TelephonyRegistry还会发个boardCast),然后回调上层APP复写方法。

(要理清关系的最好办法就是结合图阅读源码,那样会有更深的理解)

如果上面机制看不懂也没关系,作为SDK开发只要知道以下部分行,


If we want to track telephony state in 3rd application:

 We 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.                                                                //needed

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