Overriding the power button in Android

后端 未结 3 2061
陌清茗
陌清茗 2020-12-01 17:40

I\'m developing an app in which I need to do an action when the power button is pressed but unfortunately I cant handel the action of power button when it is pressed. I trie

3条回答
  •  不知归路
    2020-12-01 18:09

    I don't know what you intend to do, but you could use a BroadcastReceiver and listen for the android.intent.action.SCREEN_OFF or Intent.ACTION_SCREEN_OFF which is sent when power button is pressed (and screen is on).

    Unfortunately this intent will be sent when the screen times out too. But I think you can prevent the phone from being locked (while in an application) like this

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    

提交回复
热议问题