简体   繁体   中英

IntentService's onDestroy method getting called straight after onHandleIntent

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");

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM