0

I am developing a game. In my game i have a dialog to close and resume the game .The thing which i want is when i press the "NO" button of dialog then start counting 3 to 0 and then resume the game.Please help me Thanks.

public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {


        GamePanel.thread.setRunning(false);
        // in the next line of code we also style the dialog through xml which i put in styles
        AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
        alertDialog.setTitle("Exit Alert");
        alertDialog.setMessage("Do you really want to exit the Game?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which) {
                //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                finish();
                System.exit(0);
                return;

            } });
        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();




                GamePanel.thread.setRunning(true);
                return;
            }});
        alertDialog.show();

        return true;
    }
    return super.onKeyDown(keyCode, event);
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Mirza Zeeshan
  • 99
  • 4
  • 12
  • 1
    I don't think you should have this `System.exit(0);`. If you want a delay use a Handler . – Raghunandan Jul 27 '15 at 16:40
  • i use it for clear memory because before add this line of code i need to clear the memory of my device then my game run again. By using this line of code i don't need to clear the memory. My game works fine. – Mirza Zeeshan Jul 27 '15 at 18:29
  • From where did you get that `System.exit(0)` is the best way to clear static variables advice? It seems like an ugly hack to me! – W.K.S Jul 27 '15 at 20:01
  • http://stackoverflow.com/questions/1977246/android-finish-method-doesnt-clear-app-from-memory – Mirza Zeeshan Jul 27 '15 at 20:36

2 Answers2

1

Use CountDownTimer for that

write following lines in your NO button click evant

   new CountDownTimer(3000,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                textview.settext(millisUntilFinished);
            }

            @Override
            public void onFinish() {

            }
        };
Jaykishan Sewak
  • 822
  • 6
  • 13
0

You can use a handler to post tasks on the UI thread after a given interval of time:

alertDialog.setButton2("No",new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int which){

        dialog.dismiss();

        new CountDownTimer(3000,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                textView.setText(""+(millisUntilFinished%1000));
            }

            @Override
            public void onFinish() {
                textView.setVisibility(View.GONE);
                GamePanel.thread.setRunning(true);
            }
        };

        reurn;
    }
});
W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • can you tell me please how to show these three second delay in the form of text on screen and after three seconds game resume and text is invisible as well – Mirza Zeeshan Jul 27 '15 at 18:50
  • I've updated my answer. To use this code, you'll have to create a transparent textview over and in the centre of the game's view. – W.K.S Jul 27 '15 at 19:56
  • Ooh i understand sorry can you tell me how to craete the transpatent textview over and in the center of screen and how to use in this code thanks. – Mirza Zeeshan Jul 27 '15 at 20:27
  • i ask you for this because i am new one in android so please give me the solution thanks – Mirza Zeeshan Jul 27 '15 at 20:49