IntentService's onDestroy method getting called straight after onHandleIntent

657 views Asked by At

I have an IntentService that I am using to record audio via AudioRecord in my app. In the onHandleIntent method, I call .getInstance of a seperate class that I used for handling the recording, and then call a couple of methods of that class.

However as soon as the intentService starts, its onDestroy method gets called, and the recording stops. In the class that gets instantiated in the onHandleIntent, the audio is still recording, until the onDestroy gets called. So surely onDestroy shouldn't get called until the recording is finished.

If someone could offer a suggestion as to why it is doing this, it would be much appreciated.

The code for my IntentService is below:

public class RecordService extends IntentService {
    public File directory;
    AudioRecord audioRecord;
    int sampleRate = 44100;
    int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    File file;
    short channels = 1;
    int bitsPerSample = 16;
    public RecordService() {
        super("Record");
    }
    ExtAudioRecorder extAudioRecord;

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.v("Record", "Record called");
        extAudioRecord = ExtAudioRecorder.getInstanse(false);
        extAudioRecord.reset();
        extAudioRecord.setOutputFile(getOutputFile("RecV2").toString());
        extAudioRecord.prepare();

        extAudioRecord.start();
        //record();
    }
    public void onDestroy(){
        extAudioRecord.stop();
        //audioRecord.stop();
        Log.v("Record", "onDestroy called, Record stopped");

    }
1

There are 1 answers

1
sytu On

I believe the extAudioRecord.start(); is non-blocking meaning it returns immediately exiting onHandleIntent(). The IntentService is terminated shortly after calling onDestroy.