您的位置:首页 > 移动开发 > Android开发

android-PhoneStateListener

2013-08-27 16:44 621 查看
honeStateListener

1.对特定的电话状态的监听,包括服务的状态、信号强度、消息等待指示(语音信箱)、通话转移、呼叫状态、设备单元位置、数据连接状态、数据流量方向。一些电话信息受权限保护,应用程序不会收到受保护的信息的更新,除非在manifest文件中有适当的权限声明。凡申请许可,有适当的LISTEN_标志。

2.对监听的话做处理
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case LISTEN_SERVICE_STATE:/*Listen for changes to the network service state (cellular).对网络服务状态监听*/
PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj);
break;
case LISTEN_SIGNAL_STRENGTH:/*Listen for changes to the network signal strength (cellular).对网络信号强度变化监听*/
PhoneStateListener.this.onSignalStrengthChanged(msg.arg1);
break;
case LISTEN_MESSAGE_WAITING_INDICATOR:/*Listen for changes to the message-waiting indicator.对消息等待指示的变化监听*/
PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0);
break;
case LISTEN_CALL_FORWARDING_INDICATOR:/*Listen for changes to the call-forwarding indicator.对通话转移指示的变化监听*/
PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0);
break;
case LISTEN_CELL_LOCATION:/*Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener.对设备单元位置的变化监听,这会导致频繁的监听回调。*/
PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj);
break;
case LISTEN_CALL_STATE:/*Listen for changes to the device call state.对设备呼叫状态的变化监听。*/
PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj);
break;
case LISTEN_DATA_CONNECTION_STATE:/*Listen for changes to the data connection state (cellular).对数据连接状态的变化监听。*/
PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2);
PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1);
break;
case LISTEN_DATA_ACTIVITY:/*Listen for changes to the direction of data traffic on the data connection (cellular).对数据流量移动方向的变化监听*/
PhoneStateListener.this.onDataActivity(msg.arg1);
break;
case LISTEN_SIGNAL_STRENGTHS:/*Listen for changes to the network signal strengths (cellular).对网络信号强度的变化监听*/
PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj);
break;
}
}
};

3.监听变化后发送消息
IPhoneStateListener callback = new IPhoneStateListener.Stub() {
public void onServiceStateChanged(ServiceState serviceState) {
Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget();
}
public void onSignalStrengthChanged(int asu) {
Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget();
}
public void onMessageWaitingIndicatorChanged(boolean mwi) {
Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null)
.sendToTarget();
}
public void onCallForwardingIndicatorChanged(boolean cfi) {
Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null)
.sendToTarget();
}
public void onCellLocationChanged(Bundle bundle) {
CellLocation location = CellLocation.newFromBundle(bundle);
Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget();
}
public void onCallStateChanged(int state, String incomingNumber) {
Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget();
}
public void onDataConnectionStateChanged(int state, int networkType) {
Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType, null).
sendToTarget();
}
public void onDataActivity(int direction) {
Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget();
}
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget();
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: