emmandroid
emmandroid

Reputation: 821

How can I catch the Power Button Long press?

I am trying to develop an app that would do something after clicking the power button.

I have a code that catches the onClick of Power button, but it don't seem to work. The screen would lock.

Now I am trying to catch the onLongPress of the Power Button, preventing the "Power Off" options from appearing. Any suggestions on how to do that?

thanks in advance.

Upvotes: 1

Views: 10120

Answers (3)

user1097940
user1097940

Reputation: 283

This will surely prevent long press button.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!hasFocus) {
           Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }

Upvotes: 14

Jason Rogers
Jason Rogers

Reputation: 19344

hi your just trying to get notifed of the event than as Kartik pointed out you just need play around with the onKeyDown Event.

but form what I understand of your question you actually want to prevent the behavior of the power button and that can't be done. just like the home button pressed some events in android can't be prevented to avoid people having apps they can't quit.

Upvotes: 1

Clueless
Clueless

Reputation: 4042

You can force the screen to stay on, as described in http://developer.android.com/reference/android/os/PowerManager.html

This can drain CPU and mess with people's expectations of what happens when they hit the power button, so be careful with it.

Upvotes: 0

Related Questions