关闭应用程序后的onDestroy崩溃应用程序、onDestroy

2023-09-12 05:41:48 作者:都一样

我在制作方法的onDestroy的重写后的一些问题。 我的应用程序是一个音乐播放器,媒体播放器使用了,我需要在某一时刻,迫使它的释放,如果没有音乐正在播放的实例。 这是我的code,到目前为止,为使的伎俩,我既overrided中,onKeyDown()和的onDestroy()方法:

I'm having some problems after making an override of the method onDestroy. My app is a music player, using the instance of mediaplayer I need at some point to force the release of it if no music is playing. This is my code so far, for making the trick I've both overrided the onKeyDown() and onDestroy() method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
       if(mp.isPlaying())
       {
           //Genera la notifica
           generateNotificationSong();
           //Muovi in background
           moveTaskToBack(true);
       }
       else finish();

       return true;
    }

    return super.onKeyDown(keyCode, event);
}


//Faccio un override della funzione onDestroy per evitare che il mediaplayer continui 
//a mandare musica in background, inoltre l'UpdateTimeTask risulta inutile
@Override
public void onDestroy()
{
        mNotify.cancel(001);
        if(mHandler != null)
            mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
        if(mp != null)
            mp.release(); //rilascio il media player

        super.onDestroy();

}

就是这样,现在当我想结束我只是preSS后退按钮的应用程序和应用程序的调用方法的onPause()的onStop()和的onDestroy()吧? 反正有时会发生后关闭手机冻结4-5秒,并显示一条信息:该项目应用已关闭。 我知道我在这里做得不对,但我不知道是什么,我需要一些帮助。 谢谢指教!

That's it, now when I want to close the application I simply press the back button and the apps calls the methods onPause() onStop() and onDestroy() right? Anyway sometimes happens that after the closing the phone freeze for 4-5 seconds and a message is displayed: "The program Application has been closed". I know I'm doing something wrong here but I don't know what, I need some help. Thanks in advice!

推荐答案

super.onDestroy()必须是第一次调用的onDestroy 方法,如果您覆盖它。

super.onDestroy() must be the first call of the onDestroy method if you override it.