简体   繁体   English

在onHandleIntent之后立即调用IntentService的onDestroy方法

[英]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. 我有一个IntentService ,用于通过我的应用程序中的AudioRecord记录音频。 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. onHandleIntent方法中,我调用用于处理记录的单独类的.getInstance ,然后调用该类的几个方法。

However as soon as the intentService starts, its onDestroy method gets called, and the recording stops. 但是,一旦intentService启动,就会调用其onDestroy方法,并且记录会停止。 In the class that gets instantiated in the onHandleIntent , the audio is still recording, until the onDestroy gets called. onHandleIntent实例化的类中,音频仍在录制中,直到调用onDestroy为止。 So surely onDestroy shouldn't get called until the recording is finished. 因此,在录制完成之前,肯定不会调用onDestroy

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: 我的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");

    }

I believe the extAudioRecord.start(); 我相信extAudioRecord.start(); is non-blocking meaning it returns immediately exiting onHandleIntent() . 是非阻塞的,这意味着它会立即退出onHandleIntent() The IntentService is terminated shortly after calling onDestroy . IntentService在调用onDestroy之后不久终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntentService onCreate()调用但onHandleIntent()没有 - IntentService onCreate() called but onHandleIntent() not OnHandleIntent()没有在IntentService中调用 - OnHandleIntent() not called in IntentService intentService :为什么我的 onHandleIntent 从未被调用? - intentService : why my onHandleIntent is never called? 启动IntentService会引发NullpointerException,并且永远不会调用onHandleIntent() - Starting IntentService throws NullpointerException and onHandleIntent() is never called 方法onHandleIntent()不会被调用 - Method onHandleIntent() does not get called 是否有可能在onDestroy之后调用回调方法? - Is it possible for a callback method to be called after onDestroy? 如果未调用活动的 onDestroy() 方法,如何更新远程数据库中的状态? - How to update status in remote database if the activity's onDestroy() method is not called? 如何在IntentService中将字符串从OnStartCommand传递到OnHandleIntent? - How to pass string from OnStartCommand into OnHandleIntent in an IntentService? Android onHandleIntent IntentService等待UI线程 - Android onHandleIntent IntentService waits for UI Thread Appium:onDestroy()在测试后从未调用 - Appium: onDestroy() never called after tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM