On an Android device, where the only buttons are the volume buttons and a power button, I want to make the app react to presses on the power button (long and short). How is this done?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You could overwrite the
public boolean onKeyDown(int keyCode, KeyEvent event)
andpublic boolean onKeyUp(int keyCode, KeyEvent event)
functions in your Activity class and test ifkeyCode
is equal toKeyEvent.KEYCODE_POWER
.I haven't tested this, but I would assume the system treats this like it does the
Home
key in that you cannot stop the system from receiving the key event, you can only observe that it occurs. To test this, try returningTrue
from the above functions and see if this catches the key event.Sharing a method to listen for Power button long press. Works with API 23+ permissions:
Asking for system permission to draw overlay (This is not a normal or vulnerable permission). This is not a user permission, so You should really know, what you are doing, by asking for it.
Starting a service and adds a special view to
WindowManager
Waiting for an action inside
View
'sonCloseSystemDialogs
method.Manifest:
service_layout:
As mentioned here https://stackoverflow.com/a/15828592/1065357