Detect on/off Key Press Android

前端 未结 5 600
萌比男神i
萌比男神i 2020-12-09 13:42

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 wh

5条回答
  •  -上瘾入骨i
    2020-12-09 14:22

    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.

提交回复
热议问题