Detect on/off Key Press Android

前端 未结 5 609
萌比男神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条回答
  •  半阙折子戏
    2020-12-09 14:20

    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

提交回复
热议问题