PhoneStateListener在Nougat中无法正常工作

时间:2017-06-19 06:24:28

标签: android audio-recording video-recording phone-state-listener

我在onCreate方法中调用PhoneStateChangeListener类。

代码是:

PhoneStateChangeListener pscl = new PhoneStateChangeListener(MainActivity.this,true);
                            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                            tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);

PhoneStateChangeListener类是:

class PhoneStateChangeListener extends PhoneStateListener {


Context context;
Boolean doRecording=false;

public PhoneStateChangeListener(Context context,Boolean doRecording) {
    this.context = context;
    this.doRecording=doRecording;
}
public void onCallStateChanged(int state, String incomingNumber) {
    Log.d("CallRecorder", "PhoneListener::onCallStateChanged state:" + state + " incomingNumber:" + incomingNumber);
    //Intent callIntent = new Intent(context, RecordService.class);
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d("CallRecorder", "CALL_STATE_IDLE, stoping recording");
            Boolean stopped = context.stopService(new Intent(context, RecordService.class));
            Log.e("CallRecorder", "CALL_STATE_IDLE starting recording---------------------------------"+isMyServiceRunning((getClass())));
            Log.i("CallRecorder", "stopService for RecordService returned " + stopped);
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("CallRecorder", "CALL_STATE_RINGING");
            doRecording=false;
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            if(doRecording) {

                Log.d("CallRecorder", "CALL_STATE_OFFHOOK starting recording");
                Log.e("CallRecorder", "CALL_STATE_OFFHOOK starting recording---------------------------------" + isMyServiceRunning((getClass())));
                Intent callIntent = new Intent(context, RecordService.class);
                ComponentName name = context.startService(callIntent);
                if (null == name) {
                    Log.e("CallRecorder", "startService for RecordService returned null ComponentName");
                } else {
                    Log.i("CallRecorder", "startService returned " + name.flattenToString());
                }

            }
            break;
    }
}
}

phonestatechangelistener在nougat以下运行良好,问题是当呼叫开始时它调用CALL_STATE_OFFHOOK并且服务开始但是当呼叫结束时它不会调用CALL_STATE_IDLE因此服务没有停止这个问题出现在棉花糖之上。

1 个答案:

答案 0 :(得分:0)

Just deactivate your PhoneStateListener when your call ends:

private void deactivatePhoneStateListener(){
    if(mPhoneStateListener != null){
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
    }
    mPhoneStateListener = null;
}

Activate it again whenever you need it:

private void activatePhoneStateListener(Context context){
    mPhoneStateListener = new MyPhoneStateListener();
    mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}