如何在android中处理手机状态

时间:2013-11-27 15:46:52

标签: android

基于这三种状态:

TelephonyManager.CALL_STATE_IDLE
TelephonyManager.CALL_STATE_OFFHOOK
TelephonyManager.CALL_STATE_RINGING:

是否可以判断是否有来电?

具体来说,如果有来电,

  • 当用户接听电话时是否存在状态?
  • 通话结束的状态?
  • 是否有类似的拨出电话状态?
  • 此外,还有拒绝来电的状态吗?

1 个答案:

答案 0 :(得分:1)

你应该使用你自己的类扩展PhoneStateListener来处理调用状态的变化:

CallStateListener callListener= new CallStateListener ();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

然后,您自己的类的以下代码:

public class CallStateListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_IDLE");
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_OFFHOOK");
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_RINGING");
            break;
        }
    }

}