Reputation: 11369
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.
Upvotes: 7
Views: 23243
Reputation: 199
Building on other answers on this page, here's an approach that uses onResume and onStop... but also incorporates the Power Manager to (try to) determine whether or not the power button was indeed toggled or whether something else triggered onResume/onStop (note also that per Fraggle's observation elsewhere in this page, you may need to handle screen-off detection in onPause if you're coding for pre-KitKat versions of Android):
class ActivityMain : Activity() {
private lateinit var mvPowerManager : PowerManager
private var mvScreenOn = true
override fun onCreate(mvSavedInstanceState: Bundle?) {
super.onCreate(mvSavedInstanceState)
setContentView(R.layout.activity_main)
mvPowerManager = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
}
override fun onStop() {
super.onStop()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (!mvPowerManager.isInteractive) {
mvScreenOn = false
println("Screen turned off!")
}
}
else {
if (!mvPowerManager.isScreenOn) {
mvScreenOn = false
println("Screen turned off!")
}
}
}
override fun onResume() {
super.onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (!mvScreenOn && mvPowerManager.isInteractive) {
mvScreenOn = true
println("Screen turned on!")
}
else {
if (!mvScreenOn && mvPowerManager.isScreenOn) {
mvScreenOn = true
println("Screen turned on!")
}
}
}
}
Upvotes: 0
Reputation: 6651
To detect whether an Android app is starting or stopping, you can use these rules (reference):
Upvotes: 1
Reputation: 103
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 ()
Upvotes: 0
Reputation: 411
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.
Upvotes: 2
Reputation: 8132
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.
Upvotes: 8
Reputation: 8719
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.
Upvotes: 21