Android:手机状态在broadcast Receiver



我使用Broadcast receiver来捕捉电话状态变化。当状态第一次改变时(对于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}

当您没有呼叫时,您处于IDLE状态,当您收到呼叫时,它进入OFFHOOK状态当你的呼叫结束时,它再次进入IDLE状态

查看更多信息参考这个

如何知道我在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;
     }

最新更新