简体   繁体   中英

Override hardware volume buttons at lock-screen (device is awake)

I have searched for a solution on SO, and while this might seem to be a duplicate, most of the similar questions are asking how to detect hardware volume keys when the screen is Off . I have found suggestions of using a Broadcast Receiver but not a full explanation.

I have successfully overridden dispatchKeyEvent(KeyEvent event) method in my Activity . However, when the screen is locked (but phone is awake) these keys do not respond. Can someone please point me in the right direction to where I can detect these presses when the screen is On but still locked.

I found WindowManager.LayoutParams FLAG_SHOW_WHEN_LOCKED , which although depreciated in API 27, works well and does what I really want on my API 26 device. I put the code in my Activity's onCreate(... method right before setContentView(... .

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    this.setShowWhenLocked(true);
} else {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM