I am making an app for my project which records ongoing calls and transcribes them for people.
I am having issue with PhoneStateListener(), onCallStateChanged() and PhoneStateListener.LISTEN_CALL_STATE. These are all deprecated. Can anyone help me in running it or updating it?
public class RecordingService extends Service {
private MediaRecorder rec;
private boolean recordstarted;
private File file;
String path="/sdcard/alarms/";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//return super.onStartCommand(intent, flags, startId);
file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);
rec = new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
rec.setOutputFile(file.getAbsoluteFile()+"/"+"rec.3gp");
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
TelephonyManager manager = (TelephonyManager)getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
manager.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// super.onCallStateChanged(state, incomingNumber){
if (TelephonyManager.CALL_STATE_IDLE == state && rec == null) {
rec.stop();
rec.reset();
rec.release();
recordstarted = false;
stopSelf();
} else if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
try {
rec.prepare();
} catch (IOException e) {
throw new RuntimeException(e);
}
rec.start();
recordstarted = true;
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
return START_STICKY;
}
}
I was expecting it to work and not to be deprecated. Can anyone provide me the updated code or a way to run it?