power button event

3,160 views
Skip to first unread message

Hossein Siadati

unread,
Jul 14, 2011, 6:46:29 PM7/14/11
to android-...@googlegroups.com
Hi,

I an android app, I need to intercept event when the user presses power button on phone.
How can I do that?

Hossein

Christopher Tate

unread,
Jul 14, 2011, 6:52:53 PM7/14/11
to android-...@googlegroups.com

You can't. Applications are not told about the power or home keys.

--
christopher tate
android framework engineer

Narseo Vallina Rodriguez

unread,
Jul 14, 2011, 6:54:27 PM7/14/11
to android-...@googlegroups.com
Do you want to know when the button is pressed or when the phone is
going to be turned off?

> --
> You received this message because you are subscribed to the Google Groups "android-platform" group.
> To post to this group, send email to android-...@googlegroups.com.
> To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.
>
>

--
Narseo

Hossein Siadati

unread,
Jul 14, 2011, 6:58:33 PM7/14/11
to android-...@googlegroups.com
No. I need to intercept the power button press, so I can change the default behavior. I have already done this for Home button by manipulating a launcher(replacing the default android launcher), but I can not catch KeyEvent.KEYCODE_POWER in dispatchKeyEvent in launcher. Any idea?
Hossein

Dianne Hackborn

unread,
Jul 14, 2011, 7:04:46 PM7/14/11
to android-...@googlegroups.com
You can't.
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Hossein Siadati

unread,
Jul 14, 2011, 7:09:45 PM7/14/11
to android-...@googlegroups.com
Thanks for reply.
You mean the condition is even worse than when I intercept Home button? I have done that for Home button.
Where in the framework  I need to change in order to be able to intercept that event?
And what is the reason for having KeyEvent.KEYCODE_POWER in Android, if it is not usable?

Hossein

Dianne Hackborn

unread,
Jul 14, 2011, 7:18:47 PM7/14/11
to android-...@googlegroups.com
The power button is the one and only button the user has that an application can't screw up.  It is kind-of important.  It lets you do things like...  turn off your device.

So yes, you need to modify the framework.  This is in PhoneWindowManager.java.

Travis Armstrong

unread,
Jul 15, 2011, 1:39:19 PM7/15/11
to android-...@googlegroups.com
The Camera app on my Samsung Captivate intercepts the power button, and "locks" the camera into a completely useless mode. It is extremely frustrating and before I realized it was an intended action, I thought the phone was locked up and pulled the battery to reset it.
So I will say that there does seem to be a way to intercept the power button, but you do not want to do it to your users.

Hossein Siadati

unread,
Jul 30, 2011, 7:13:13 AM7/30/11
to android-...@googlegroups.com
Hi,
Thanks for replies.
I could manage to catch the power button(not intercept), and that would be enough for our test. I use BroadcastReceiver, to hook on the power press event.
In addition, I add these flags to my Activity in order to prevent the screen from being locked when the power button is pressed:
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//prevent application from being locked
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //I am not sure it works.
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//turns the screen on
The last too, help me to keep the screen on(with a slight unwanted things that I describe).
The problem is that when the user press the power button, the screen turns off for about 1 second and the turns on  automatically again.
Is there anyway I can prevent the screen off when user presses the power button? In that case, I can handle power press event like any other event.
(I am developing for a Nexus S, if Droid version is matter!)

Regards, specially to Dianne,
Reply all
Reply to author
Forward
0 new messages