美文网首页
android 9.0 获取去电时对方接通的真实状态(监听通话过

android 9.0 获取去电时对方接通的真实状态(监听通话过

作者: 大胡子的机器人 | 来源:发表于2019-10-10 10:57 被阅读0次

场景:app是完全接管了系统的拨号及通话页面,因此很多状态无法获取,就比如去电时判断对方是否真实接通还是处于对方振铃状态。
在应用层监听通话状态只有三种,从TelephonyManager.java中注释可知这三种状态含义如下:
CALL_STATE_IDLE :空闲态(没有通话活动)
CALL_STATE_RINGING :包括响铃、第三方来电等待
CALL_STATE_OFFHOOK 摘机(接听):包括dialing拨号中、active接通、hold挂起等

<!--   清单文件中, 申明监听通话精确状态权限,该权限需要android:sharedUserId="android.uid.system"  -->
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE" />

监听通话状态:

mReceiver = new MobileCallReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    filter.addAction(CustomAction.PRECISE_CALL_STATE);
    registerReceiver(mReceiver, filter);
  /******************** 此处省略一万行 ************************/
 class MobileCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
            return;
        }
        switch (intent.getAction()) {
            case CustomAction.PRECISE_CALL_STATE:
                int callState = intent.getIntExtra("foreground_state", -2);
                switch (callState) {
                    case PreciseCallState.PRECISE_CALL_STATE_IDLE:
                        Log.d(TAG, "IDLE");
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_DIALING:
                        Log.d(TAG, "DIALING");
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_ALERTING:
                        Log.d(TAG, "ALERTING isHandFree=" + isHandFree);
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_ACTIVE:
                        Log.d(TAG, "ACTIVE");
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_INCOMING:
                        Log.d(TAG, "INCOMING来电");
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING:
                        Log.d(TAG, "DISCONNECTING");
                        break;
                    case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED:
                        Log.d(TAG, "DISCONNECTED");
                        break;
                }
                break;
            case TelephonyManager.ACTION_PHONE_STATE_CHANGED:
                break;
        }
    }
}

// 精确的九大通话状态
public class PreciseCallState {
    public static final int PRECISE_CALL_STATE_IDLE = 0; //通话空闲
    public static final int PRECISE_CALL_STATE_ACTIVE = 1; //正在通话(活动中)
    public static final int PRECISE_CALL_STATE_HOLDING = 2; //通话挂起(例如我和多个人通话,其中一个通话在活动,而其它通话就会进入挂起状态)
    public static final int PRECISE_CALL_STATE_DIALING = 3; //拨号开始
    public static final int PRECISE_CALL_STATE_ALERTING = 4; //正在呼出(提醒对方接电话)
    public static final int PRECISE_CALL_STATE_INCOMING = 5; //对方来电
    public static final int PRECISE_CALL_STATE_WAITING = 6; //第三方来电等待(例如我正在和某人通话,而其他人打入时就会就进入等待状态)
    public static final int PRECISE_CALL_STATE_DISCONNECTED = 7; //挂断完成
    public static final int PRECISE_CALL_STATE_DISCONNECTING = 8; //正在挂断
}

参考文章:
https://blog.csdn.net/qq_32115439/article/details/78395537

相关文章

网友评论

      本文标题:android 9.0 获取去电时对方接通的真实状态(监听通话过

      本文链接:https://www.haomeiwen.com/subject/ozsopctx.html