1

My question changed. can I do the same thing using volume button instead? (that is, trigger act2 even when the the phone is sleeping)


Hi I want to write an app making use of the power button. The concept of the app is as follows

Activity1 -->service1-->service2-->service3-->.....-->serviceXX-->Activity2

each class has its won trigger mechanism. what I want to do is : no matter when(whether you started the app or not) , press the power button quickly 5 times (5 times in 1 second,not accumulated to 5 times) , and it is forced to jump to Activity2. Of course, the original power button function is still maintained, I just want to add an special function to power button. Atfer I stop the app, I want the power function settings stilled memorized by the phone.

Is it possible to do so? Thanks for help!

Jim31837
  • 1,589
  • 3
  • 12
  • 12
  • why the power button? Your questions title suggests the volume button.... why not use that. – Ravi Vyas Jun 12 '11 at 17:43
  • at first, I ask about power button, and the answers down there let me know it's not possible. So I change the question into "volume button" – Jim31837 Jun 12 '11 at 19:22
  • Access this: https://stackoverflow.com/questions/2874743/android-volume-buttons-used-in-my-application – Robson Chico Mar 29 '21 at 13:55

1 Answers1

6

I want to write an app making use of the power button.

Fortunately, this is not possible.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer! Is it possible using volume button instead? – Jim31837 Jun 12 '11 at 17:32
  • @Jim31837: No. There is no button that always exists on every device that can be intercepted by a `Service` or `BroadcastReceiver`. An `Activity` can respond to just about everything (HOME, end-call, and power being three notable exceptions) in `onKeyDown()`, but you indicated that "whether you started the app or not", which I am assuming means "whether one of my activities is in the foreground". – CommonsWare Jun 12 '11 at 17:46
  • @Jim31837: Devices that happen to have a CAMERA button will send a broadcast `Intent` when that is pressed if the foreground `Activity` does not use it, but there is no way you can use that for "5 times in 1 second". – CommonsWare Jun 12 '11 at 17:46
  • So I can say that when my screen is off, any KeyEvent will not be detected. Is that correct? – Jim31837 Jun 12 '11 at 19:28
  • @Jim31837: Absolutely. That is true even if your activity is in the foreground. – CommonsWare Jun 12 '11 at 20:00
  • Thanks a lot for your advices! I really appretiate that. – Jim31837 Jun 12 '11 at 20:18
  • Can we use this [link](http://developer.android.com/training/managing-audio/volume-playback.html) to control the volume key functionality? @CommonsWare – Behzad Aug 01 '13 at 22:59