0
這裏我在onCreate方法中調用PhoneStateChangeListener類。PhoneStateListener在Nougat中無法正常工作
代碼:
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,因此服務不會停止。這個問題在棉花糖上面。