android PhoneStateListener onCallStateChanged奇怪的行为

时间:2018-11-21 08:49:45

标签: java android android-studio telephonymanager phone-state-listener

美好的一天。我正在做一个简单的演示,我需要在DB中存储当前的呼叫状态。我在onCreate中使用此代码

for (i in marketsFilter.indices) {
    val market = marketsFilter[i]
    marketTypes.add(MyApplication.allMarketTypes.single { 
        it.id == market.marketTypeId 
    })
}

奇怪的是,当它处于振铃状态时,它不会在数据库中更新,而在摘机状态中它会更新,但是在调用后不会更新回到空闲状态。 这是我的日志:

telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    Log.i(TAG, "onCallStateChanged: RING = "+state);

                        updateState(token, String.valueOf(state));

                }
                if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    Log.i(TAG, "onCallStateChanged: CALL = "+state);

                        updateState(token, String.valueOf(state));
                }

                if (state == TelephonyManager.CALL_STATE_IDLE) {
                    Log.i(TAG, "onCallStateChanged: IDLE = "+state);

                        updateState(token, String.valueOf(state));
                }
            }
        };
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

权限:

2018-11-21 14:23:59.225 1560-1560/? D/GpsNetInitiatedHandler: onCallStateChanged(): state is 0
2018-11-21 14:23:59.226 1560-1560/? D/RilInfoMonitor: onCallStateChanged(): state is 0
2018-11-21 14:23:59.227 16884-16884/com.aliaskarurakov.android.sendcall I/android.app.Activity: onCallStateChanged: IDLE = 0
2018-11-21 14:34:31.477 17892-17892/com.aliaskarurakov.android.sendcall I/android.app.Activity: onCallStateChanged: IDLE = 0
2018-11-21 14:35:33.606 17892-17892/com.aliaskarurakov.android.sendcall I/android.app.Activity: onCallStateChanged: RING = 1
2018-11-21 14:35:33.609 1560-1560/? D/GpsNetInitiatedHandler: onCallStateChanged(): state is 1
2018-11-21 14:35:33.609 1560-1560/? D/RilInfoMonitor: onCallStateChanged(): state is 1
2018-11-21 14:35:52.874 1560-1560/? D/GpsNetInitiatedHandler: onCallStateChanged(): state is 0
2018-11-21 14:35:52.876 17892-17892/com.aliaskarurakov.android.sendcall I/android.app.Activity: onCallStateChanged: IDLE = 0
2018-11-21 14:35:52.878 1560-1560/? D/RilInfoMonitor: onCallStateChanged(): state is 0
2018-11-21 14:36:01.375 1560-1560/? D/GpsNetInitiatedHandler: onCallStateChanged(): state is 2
2018-11-21 14:36:01.379 1560-1560/? D/RilInfoMonitor: onCallStateChanged(): state is 2
2018-11-21 14:36:01.382 17892-17892/com.aliaskarurakov.android.sendcall I/android.app.Activity: onCallStateChanged: CALL = 2
2018-11-21 14:36:19.502 1560-1560/? D/GpsNetInitiatedHandler: onCallStateChanged(): state is 0
2018-11-21 14:36:19.503 1560-1560/? D/RilInfoMonitor: onCallStateChanged(): state is 0

1 个答案:

答案 0 :(得分:0)

不幸的是,我的日志看上去也遇到了同样的问题

小米手机

CallReceiver onCallStateChanged   state - 1
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 2
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0

三星手机

CallReceiver onCallStateChanged   state - 1
CallReceiver onCallStateChanged   state - 1
CallReceiver onCallStateChanged   state - 1
CallReceiver onCallStateChanged   state - 2
CallReceiver onCallStateChanged   state - 2
CallReceiver onCallStateChanged   state - 2
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0
CallReceiver onCallStateChanged   state - 0 

状态1 =振铃,2 =连接,0 ==空闲 我已经开始在联播中进行录制,但是它会立即在xiomi mobile上停止(与Samsung 9完美配合),因为它在联播后立即调用空闲状态,而无需等待用户中断呼叫

我发现自己的错误实际上是我多次在onReceive()中注册侦听器,所以我将代码更改为仅初始化一次以静态方式保留实例,如果不为null,则下次使用它

       static phoneListener 
       if(phoneListener==null){
                phoneListener = PhoneCallStateListener(context, intent)
                val telephony = context
                        .getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE)
        }