IntentService 的 onDestroy 方法在 onHandleIntent 之后直接调用



我有一个IntentService,用于通过应用程序中的AudioRecord录制音频。在onHandleIntent方法中,我调用用于处理录制的单独类的.getInstance,然后调用该类的几个方法。

但是,intetService一启动,它的onDestroy方法就会被调用,录制就会停止。在onHandleIntent中实例化的类中,音频仍在录制,直到调用onDestroy为止。因此,在录制完成之前,肯定不应该调用onDestroy

如果有人能就它为什么这么做提出建议,我们将不胜感激。

我的IntentService的代码如下:

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

我相信extAudioRecord.start();是非阻塞的,这意味着它会立即返回并退出onHandleIntent()。IntentService在调用onDestroy后不久终止。

相关内容

最新更新