Android:广播接收器中的电话状态

时间:2011-07-18 05:52:12

标签: android broadcastreceiver

我使用广播接收器来捕捉手机状态变化。 当状态在第一次改变时(对于State_OffHook),它工作正常,但在呼叫结束时不作出反应。 这是我的代码:

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                     
            if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {working fine}
                else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {doesn't react}

1 个答案:

答案 0 :(得分:0)

当您没有通话时,您处于IDLE状态,当您接听电话时,它会转到OFFHOOK州 当你的通话结束时,它再次进入IDLE

了解更多信息 参考这个

How to know whether I am in a call on Android?

修改

@Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
                    //Here you are came from offhook because value of UDF.phoneState != TelephonyManager.CALL_STATE_IDLE
                    //IDLE is calls many times so you have to keep track by a static variable like  UDF.phoneState
                } 
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                 //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                endCallIfBlocked(incomingNumber);
                break;

            default:
                break;
        }
        UDF.phoneState = state;
     }