Tamim
Tamim

Reputation: 181

libgdx how to access power button of android device?

I just want to check that the power button is pressed or not in LibGDX and if it is pressed then exit/close the game or something else like change screen. I used this code:

@Override
public void render(SpriteBatch sb) {

    sb.begin();

    if(Gdx.input.isKeyPressed(Input.Keys.POWER)){
        Gdx.app.exit();
    }
    sb.end();

}

but this is not working. After I press the button screen turns off and when I turn it back on the game resumes from exactly where I left it. I wanted to keep the screen on on power button press but I didn't find any solution on that. Now I at least want to access the button and then do something on button press.

the same code works for volume up/down button. Code:

@Override
public void render(SpriteBatch sb) {

    sb.begin();


    if(Gdx.input.isKeyPressed(Input.Keys.VOLUME_UP)){
        Gdx.app.exit();
    }
    sb.end();

}

Please describe if I'm doing anything wrong and what shall I do.

Upvotes: 1

Views: 127

Answers (1)

icarumbas
icarumbas

Reputation: 1815

You can do all saves in pause method that is available in Screen and ApplicationListener classes. It will be called when you exit the app or block device.

@Override
public void pause () {
      // save here
}

Upvotes: 1

Related Questions