I read some posts here on Stackoverflow, and I did not find a good solution, I wonder if it is possible to determine when the user presses the power button for a long time while trying to turn off the device, I would like to find out if you can detect this event and enable or disable showing this dialog box in which it appears (reboot, shutdown, etc.)
I tried this:
@Override public boolean dispatchKeyEvent(KeyEvent event) { Toast.makeText(MainActivity.this, event.getKeyCode(), Toast.LENGTH_SHORT).show(); return true; }
but it does not appear, it should also work as a service, I mean that the application may or may not be open to show this toast.
EDIT
This is how I put onCloseSystemDialog
//home or recent button public void onCloseSystemDialogs(String reason) { if ("globalactions".equals(reason)) { Toast.makeText(PowerButtonService.this, "yaps", Toast.LENGTH_SHORT).show(); Intent i= new Intent(getBaseContext(), Demo.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(i); //} else if ("homekey".equals(reason)) { //home key pressed //} else if ("recentapss".equals(reason)) { // recent apps button clicked } }
It works fine, but only when the device is unlocked, when the device is locked, nothing is displayed.
I am also trying to figure out how to remove the dialog box when the user presses the power button. I tried to do this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
But if I want to show it again, how can I do it?
android events android-studio
Skizo-ozᴉʞS Aug 21 '16 at 13:06 on 2016-08-21 13:06
source share