0
ここでは、onCreateメソッドでPhoneStateChangeListenerクラスを呼び出しています。NougatでPhoneStateListenerが正しく動作しない
コードは次のとおりです。
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がうまくヌガーの下に働いているコールは、それがCALL_STATE_OFFHOOKを呼び出して起動し、サービスが開始されるとき、問題があるが、呼び出しが終了するとCALL_STATE_IDLEが呼び出されず、サービスは停止しませんこの問題はマシュマロの上にあります。通話が終了したときに