Android即将离职的Call Ringing Stage

时间:2017-07-06 05:43:50

标签: android broadcastreceiver phone-call notification-listener

我试图接到拨出电话的响铃阶段。我已经尝试使用NEW_OUTGOING_CALL和PHONE_STATE动作播放BroadcastReceiver。但我没有得到接收器的振铃阶段。我现在明白,接收器并没有告诉你振铃的阶段。我看到过同样的问题: - Android - How to detect outgoing call is answered or received? 我也应用了这个解决方案,但我仍然没有得到任何解决方案。我已经使用NotificationListenerService操作声明了服务,我需要做额外的事情或者别的......

我认为要在拨出电话的响铃阶段或在另一端接听电话这是可能的..

请帮忙!

1 个答案:

答案 0 :(得分:0)

在清单文件中,您应具有以下权限来检查拨出电话:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

和广播接收器的代码:

public class PhoneStateChecking extends BroadcastReceiver {
    boolean hasCallStateRinging = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("Intent", intent.toString());
        if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            Log.e("CAll-STATUS", "out going Call");
        }

        //        Checking for the call status
        try {
            // TELEPHONY MANAGER class object to register one listner
            TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            //Create Listner
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    if (hasCallStateRinging)
                        return;
                    else {
                        hasCallStateRinging = true;
                        Log.e("CAll-STATUS", "CALL_STATE_RINGING");
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    if(!hasCallStateRinging){
                        hasCallStateRinging = true;
                        Log.e("CAll-STATUS", "CALL_STATE_OFFHOOK");
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if (hasCallStateRinging) {
                        hasCallStateRinging = false;
                        Log.e("CAll-STATUS", "CALL_STATE_IDLE");
                    }
                    break;
            }
        }
    }

}