6

How can I detect the power button or lock screen button being pressed? When my game is paused in this way, it can cause the game to crash because I need to pause a thread when it happens.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
DRiFTy
  • 11,269
  • 11
  • 61
  • 77
  • Chances are your problem is better solved by correctly implementing the activity lifecycle, in particular taking the appropriate actions in response to the various messages. – Chris Stratton Jul 27 '11 at 17:20
  • 2
    You should not be looking for key presses to fix your app. The Android lifecycle is a much better choice, and should solve the issue of pausing your game when the user exits or pauses it. Have you tried overriding `onPause` in your game's `Activity`? – Austyn Mahoney Jul 27 '11 at 17:21
  • I have done this, but I think I am handling my threads incorrectly at the moment. I will have to go back and maybe try managing them in different methods, and then write the code for handling the lockscreen issue in the onPause method – DRiFTy Jul 27 '11 at 17:34

5 Answers5

20

From Christian's answer to this question:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

However Jake Basile is right. Unless you have a really good reason for doing something special when the Power Button is pressed, you should be using standard Android life-cycle functions.

When the Power Button is pressed it will call the onPause() method of your application, and when you unlock the device it will call onResume(). This is where you should be managing your thread to prevent the app from crashing.

The documentation on Activity's will give you a detailed description of life-cycle functions, when they are called, and how you should use them. enter image description here

Community
  • 1
  • 1
theisenp
  • 8,639
  • 5
  • 39
  • 47
8

Use the onPause and onResume methods. These are called when your app is no longer in the foreground (when a lock screen comes up) or when the phone is put to sleep, and when your app is brought back to the foreground.

This is the standard process for the Activity life cycle in Android, that is more fully documented here.

jakebasile
  • 8,084
  • 3
  • 28
  • 34
  • I had a feeling this was going to be how to do it. The only problem is right now I am already using these methods to kill my threads and bring the game screen back to the menu when the user wants to quit the game... So that must be why the game is crashing completely when this button is pressed... Hmmm might have to handle things differently. – DRiFTy Jul 27 '11 at 17:25
  • You could try moving them to `onStart` and/or `onStop`, and see what happens. It might need to be earlier or later in the lifecycle to avoid thread issues. – jakebasile Jul 27 '11 at 17:26
  • Thanks I will try that, having multiple threads running always seems to get fussy... I would accept your answer but the other answer here seems to answer the question I was asking even though I realize I should be implementing my thread management using Android activity lifecycle methods differently... – DRiFTy Jul 27 '11 at 17:30
2

A lot of the answers to this question ignore the fact that when writing an app, it is important to know which event caused a particular standard Android life cycle function to be called. There is a onBackPressed() function; why not have an onPowerPressed() and onHomePressed() as well? Apps often need to do different things in the standard life cycle functions depending upon which event caused them to be called. Right now it is a real pain to figure out whether a standard life cycle method was called as a result of a home button press or a power button press.

Chris B
  • 401
  • 6
  • 10
1

To detect whether an Android app is starting or stopping, you can use these rules (reference):

  • When the user is entering, onResume is always called (on the activity that will be current)
  • When the user is leaving, onStop is always called (on the current activity), except when pressing the power button (to turn off) where you may only see an onPause call.
  • When the user is moving between activities, onResume is always called on the new activity, and onStop is always called on the old one. The onResume call is always before onStop
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Andy Balaam
  • 6,423
  • 6
  • 34
  • 37
  • @Jean-François Fabre did you feel the link I included was unhelpful? It provides the evidence behind the answer I gave, and more detail so I hoped it was helpful. – Andy Balaam Nov 25 '19 at 09:14
  • I removed it because links at the end of answers (specially to a blog) are considered borderline spam – Jean-François Fabre Nov 25 '19 at 09:24
0

Override the life cycle methods. The solution for detecting the pressing of the power button that was chosen as the answer is no longer allowed in Android ()