onDestroy crash after closing app

岁酱吖の 提交于 2019-12-11 05:33:36

问题


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

}

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!


回答1:


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




回答2:


Try the below code it works for me

@Override

public void onDestroy() {

    mediaPlayer.stop();

    super.onDestroy();

        }

}



来源:https://stackoverflow.com/questions/16152354/ondestroy-crash-after-closing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!