关闭

Android应用开发之PhoneStateListener 的使用

标签: android应用网络
2135人阅读 评论(0) 收藏 举报

这两天在做翻转静音的功能,需要用到PhoneStateListener,以前只是知道有这么个东西,没有具体用过

包含此类的包是:android.telephony.PhoneStateListener

 

由于StatusBar中用到了PhoneStateListener中较多的内容,索性研究了一下StatusBarPolicy.java

76 /**
77  * This class contains all of the policy about which icons are installed in the status
78  * bar at boot time.  In reality, it should go into the android.policy package, but
79  * putting it here is the first step from extracting it.
80  */

services/java/com/android/server/status/StatusBarPolicy.java

 

 

1、首先需要通过TelephonyManager来注册要监听的状态,状态定义在类PhoneStateListener中,如下所示

1)   LISTEN_CALL_FORWARDING_INDICATOR      Listen for changes to the call-forwarding indicator.

Requires Permission: READ_PHONE_STATE

2)   LISTEN_CALL_STATE                                      Listen for changes to the device call state.

Requires Permission: READ_PHONE_STATE

3)   LISTEN_CELL_LOCATION                               Listen for changes to the device's cell location. Note that this

                                                                            will result in frequent callbacks to the listener.

Requires Permission: ACCESS_COARSE_LOCATION

If you need regular location updates but want more control over the update interval or location precision, you can set up a listener through the location manager instead.

4)   LISTEN_DATA_ACTIVITY                                 Listen for changes to the direction of data traffic on the data

                                                                            connection (cellular).

Requires Permission: READ_PHONE_STATE Example: The status bar uses this to display the appropriate data-traffic icon.

5)   LISTEN_DATA_CONNECTION_STATE              Listen for changes to the data connection state (cellular).

6)   LISTEN_MESSAGE_WAITING_INDICATOR       Listen for changes to the message-waiting indicator.

Requires Permission: READ_PHONE_STATE

Example: The status bar uses this to determine when to display the voicemail icon.

7)   LISTEN_NONE                                                Stop listening for updates.

8)   LISTEN_SERVICE_STATE                                Listen for changes to the network service state (cellular).

9)   LISTEN_SIGNAL_STRENGTH                           This constant is deprecated. by LISTEN_SIGNAL_STRENGTHS

10) LISTEN_SIGNAL_STRENGTHS                         Listen for changes to the network signal strengths (cellular).

Example: The status bar uses this to control the signal-strength icon

 

 

StatusBarPolicy中的注册代码如下所示:

438         // register for phone state notifications.
439         ((TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE))
440                 .listen(mPhoneStateListener,
441                           PhoneStateListener.LISTEN_SERVICE_STATE
442                         | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
443                         | PhoneStateListener.LISTEN_CALL_STATE
444                         | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
445                         | PhoneStateListener.LISTEN_DATA_ACTIVITY);

 

当不需要再继续监听上述状态时,通常需要注销掉

// unregister for phone state notifications.

((TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE))
        .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
 
 
2、定义mPhoneStateListener,以内部匿名类的方式定义
885     private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
886         @Override
887         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
888             mSignalStrength = signalStrength;
889             updateSignalStrength();
890         }
892         @Override
893         public void onServiceStateChanged(ServiceState state) {
894             mServiceState = state;
895             updateSignalStrength();
896             updateCdmaRoamingIcon(state);
897             updateDataIcon();
898         }
900         @Override
901         public void onCallStateChanged(int state, String incomingNumber) {
902             updateCallState(state);
903             // In cdma, if a voice call is made, RSSI should switch to 1x.
904             if (isCdma()) {
905                 updateSignalStrength();
906             }
907         }
909         @Override
910         public void onDataConnectionStateChanged(int state, int networkType) {
911             mDataState = state;
912             updateDataNetType(networkType);
913             updateDataIcon();
914         }
916         @Override
917         public void onDataActivity(int direction) {
918             mDataActivity = direction;
919             updateDataIcon();
920         }
921     };
 

1) public void onSignalStrengthsChanged(SignalStrength signalStrength)

// Callback invoked when network signal strengths changes.

通过函数updateSignalStrength更新StatusBar上的信号图标


2) public void onServiceStateChanged(ServiceState state)

// Callback invoked when device service state changes.

通过ServiceState返回当前服务状态,有如下四种状态

STATE_EMERGENCY_ONLY       The phone is registered and locked. Only emergency numbers are allowed.

STATE_IN_SERVICE                  Normal operation condition, the phone is registered with an operator either in

                                                home network or in roaming.

STATE_OUT_OF_SERVICE        Phone is not registered with any operator, the phone can be currently searching a

                                                new operator to register to, or not searching to registration at all, or registration

                                                is denied, or radio signal is not available.

STATE_POWER_OFF                 Radio of telephony is explicitly powered off.

 

同时还可以获取到以下信息

  • Service state: IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF
  • Roaming indicator
  • Operator name, short name and numeric id
  • Network selection mode

     

    StatusBarPolicy根据上面四种状态封装了如下函数,用来判断当前是否有服务

    960     private boolean hasService() {
    961         if (mServiceState != null) {
    962             switch (mServiceState.getState()) {
    963                 case ServiceState.STATE_OUT_OF_SERVICE:
    964                 case ServiceState.STATE_POWER_OFF:
    965                     return false;
    966                 default:
    967                     return true;
    968             }
    969         } else {
    970             return false;
    971         }
    972     }
    3) public void onCallStateChanged(int state, String incomingNumber)
    // Callback invoked when device call state changes
    用来更新来电图标,其中state有三种状态,如下

    public static final int CALL_STATE_IDLE

    Since: API Level 1

    Device call state: No activity.

    Constant Value: 0 (0x00000000)

    public static final int CALL_STATE_OFFHOOK

    Since: API Level 1

    Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.

    Constant Value: 2 (0x00000002)

    public static final int CALL_STATE_RINGING

    Since: API Level 1

    Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.

    Constant Value: 1 (0x00000001)
    4) public void onDataConnectionStateChanged(int state, int networkType)
    // Callback invoked when connection state changes
    用来控制是否显示有移动网络数据交互,其中state有四种状态,如下

    public static final int DATA_CONNECTED

    Since: API Level 1

    Data connection state: Connected. IP traffic should be available.

    Constant Value: 2 (0x00000002)

    public static final int DATA_CONNECTING

    Since: API Level 1

    Data connection state: Currently setting up a data connection.

    Constant Value: 1 (0x00000001)

    public static final int DATA_DISCONNECTED

    Since: API Level 1

    Data connection state: Disconnected. IP traffic not available.

    Constant Value: 0 (0x00000000)

    public static final int DATA_SUSPENDED

    Since: API Level 1

    Data connection state: Suspended. The connection is up, but IP traffic is temporarily unavailable. For example, in a 2G network, data activity may be suspended when a voice call arrives.

    Constant Value: 3 (0x00000003)
    5) public void onDataActivity(int direction)
    // Callback invoked when data activity state changes.
    用来控制显示数据传输的图标
    918             mDataActivity = direction;
    1115                     switch (mDataActivity) {
    1116                         case TelephonyManager.DATA_ACTIVITY_IN:
    1117                             iconId = mDataIconList[1];
    1118                             break;
    1119                         case TelephonyManager.DATA_ACTIVITY_OUT:
    1120                             iconId = mDataIconList[2];
    1121                             break;
    1122                         case TelephonyManager.DATA_ACTIVITY_INOUT:
    1123                             iconId = mDataIconList[3];
    1124                             break;
    1125                         default:
    1126                             iconId = mDataIconList[0];
    1127                             break;
  • 0
    0

    查看评论
    * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
      个人资料
      • 访问:128631次
      • 积分:1758
      • 等级:
      • 排名:千里之外
      • 原创:22篇
      • 转载:112篇
      • 译文:8篇
      • 评论:13条
      最新评论