按Home键

时间:2015-07-29 10:54:42

标签: java android onresume onpause android-homebutton

我正在开发一款游戏。当我按下设备上的主页按钮时,我的游戏就关闭了。当我再次启动它时,我的游戏才开始,直到我清除了设备的内存。

注意:我想要实现的是,当我开始游戏时,按下主页按钮后,应该先启动它而不先清除内存。它应该从我离开的地方开始。请帮我。提前谢谢。

这是我的mainActivity的代码:

public class Game extends Activity {
    MediaPlayer backgroungMusic;
    Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     //   setContentView(R.layout.activity_game);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


        //turn title off
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(new GamePanel(this));

        backgroungMusic = MediaPlayer.create(Game.this, R.raw.music);
        backgroungMusic.setLooping(true);
        backgroungMusic.start();

    }


    @Override
    protected void onPause()
    {
        super.onPause();
        backgroungMusic.release();
        finish();
    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {


            //  GamePanel.thread.setStoped(true);
            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("Quit", 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 the static variables. It will give you some free space.
                    // A lot of applications leave working processes and variables which makes me angry. After 30 minutes of usage the memory is full and I have to run the Task Manager - Lvl 2 clear memory
                    finish();
                    System.exit(0);
                    return;

                }
            });


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

                    // dialog.cancel();
                    // GamePanel.thread.resume();
                    dialog.dismiss();
                    // When user presses the "Cancle" button then the game resumes for 3 seconds and then start again.
                    // Here is the Code of the toasts and each toast appears with a delay of one second.


                    return;
                }
            });


            alertDialog.show();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案