Use of Wakelok and Stand-By in Android Games

When to suppress the Stand-By Mode in Games?

This depends on the kind of game you want to develop. If the player has to interact with the game constantly or if he only has a limited time to draw, the stand-by mode usually doesn’t need to be disabled.

It’s relevant to disable the stand-by mode only in video-or demonstration modes, when the player doesn’t need to interact at all for a certain time. It’s more important, how the game reacts on the stand-by mode. It’s the best solution if a game automatically switches to the pause-menu on a stand-by, where the player can easily resume the game.

How to suppress the Stand-By Mode?

You use so called “Wakelocks” to suppress the stand-by mode on a Android Phone. Here I show you a sample implementation using the game-engine e3roid. It’s very important to release the Wakelock before exiting the game.

Manifest:

You need to request the nessesary permision:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Main Class:

public class StoneSetter extends E3Activity {
private PowerManager.WakeLock wl;
public E3Scene onLoadScene() {
   ...
   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "SomeTag");
   wl.acquire();
}
 
public boolean onKeyDown(E3Scene in_scene, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK)){
   exitGame();
   return true;
}else{
   return false;
}
}
 
public void exitGame(){
  wl.release();
  finish();
}
 
}