PhoneStateListener
我们可以通过
TelephoneManager 服务来 监听 电话的状态
1: private TelephonyManager tm;
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
2: 写一个类继承 PhoneStateListener
3:重写 @Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
}
方法
4:电话状态
TelephonyManager.CALL_STATE_IDLE: 手机空闲起来了
TelephonyManager.CALL_STATE_RINGING: 手机铃声响了,
来电号码incomingNumber;
TelephonyManager.CALL_STATE_OFFHOOK: 电话被挂起了
注册事件
5: tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
6:取消事件
tm.listen(listener, PhoneStateListener.LISTEN_NONE);
listener = null;
[java]
public void onServiceStateChanged(ServiceState serviceState)
public void onMessageWaitingIndicatorChanged(boolean mwi)
public void onCallForwardingIndicatorChanged(boolean cfi)
public void onCellLocationChanged(CellLocation location)
public void onCallStateChanged(int state, String incomingNumber)
public void onDataConnectionStateChanged(int state)
public void onDataConnectionStateChanged(int state, int networkType)
public void onDataActivity(int direction)
public void onSignalStrengthsChanged(SignalStrength signalStrength)
public void onServiceStateChanged(ServiceState serviceState)
public void onMessageWaitingIndicatorChanged(boolean mwi)
public void onCallForwardingIndicatorChanged(boolean cfi)
public void onCellLocationChanged(CellLocation location)
public void onCallStateChanged(int state, String incomingNumber)
public void onDataConnectionStateChanged(int state)
public void onDataConnectionStateChanged(int state, int networkType)
public void onDataActivity(int direction)
public void onSignalStrengthsChanged(SignalStrength signalStrength)
2)int events
Events取值如下:
[java]
public static final int LISTEN_NONE = 0; //停止监听
public static final int LISTEN_SERVICE_STATE = 0x00000001;
public static final int LISTEN_MESSAGE_WAITING_INDICATOR = 0x00000004;
public static final int LISTEN_CALL_FORWARDING_INDICATOR = 0x00000008;
public static final int LISTEN_CELL_LOCATION = 0x00000010;
public static final int LISTEN_CALL_STATE = 0x00000020;
public static final int LISTEN_DATA_CONNECTION_STATE = 0x00000040;
public static final int LISTEN_DATA_ACTIVITY = 0x00000080;
public static final int LISTEN_SIGNAL_STRENGTHS = 0x00000100;
public static final int LISTEN_NONE = 0; //停止监听
public static final int LISTEN_SERVICE_STATE = 0x00000001;
public static final int LISTEN_MESSAGE_WAITING_INDICATOR = 0x00000004;
public static final int LISTEN_CALL_FORWARDING_INDICATOR = 0x00000008;
public static final int LISTEN_CELL_LOCATION = 0x00000010;
public static final int LISTEN_CALL_STATE = 0x00000020;
public static final int LISTEN_DATA_CONNECTION_STATE = 0x00000040;
public static final int LISTEN_DATA_ACTIVITY = 0x00000080;
public static final int LISTEN_SIGNAL_STRENGTHS = 0x00000100;