这两天在做翻转静音的功能,需要用到PhoneStateListener,以前只是知道有这么个东西,没有具体用过
包含此类的包是:android.telephony.PhoneStateListener
由于StatusBar中用到了PhoneStateListener中较多的内容,索性研究了一下StatusBarPolicy.java
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中的注册代码如下所示:
当不需要再继续监听上述状态时,通常需要注销掉
// unregister for phone state notifications.
1) public void onSignalStrengthsChanged(SignalStrength signalStrength)
// Callback invoked when network signal strengths changes.
通过函数updateSignalStrength更新StatusBar上的信号图标
2) public void onServiceStateChanged(ServiceState state)
通过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 1Device call state: No activity.
Constant Value: 0 (0x00000000)public static final int CALL_STATE_OFFHOOK
Since: API Level 1Device 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 1Device 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 1Data connection state: Connected. IP traffic should be available.
Constant Value: 2 (0x00000002)public static final int DATA_CONNECTING
Since: API Level 1Data connection state: Currently setting up a data connection.
Constant Value: 1 (0x00000001)public static final int DATA_DISCONNECTED
Since: API Level 1Data connection state: Disconnected. IP traffic not available.
Constant Value: 0 (0x00000000)public static final int DATA_SUSPENDED
Since: API Level 1Data 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;