Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - blocking hardware buttons while locked
    primarykey
    data
    text
    <p>I'm looking into making an app that prevents the default action on volume up / down key presses, and would like to get some input to see if I'd be wasting my time trying.</p> <p>Here's the setup:</p> <p>I have a Samsung Galaxy S3. I like to listen to music. Often i unintentionally change the volume of whats playing when the phone is locked and in my pocket.</p> <p>At first i thought it would be simple; having tested a simple override of the onKeyDown() method for retrieving the integer values for the volume keys. Quickly i came to the realization that this would only work if i were to not lock my phone and keep ONLY that app open.</p> <p>Next i found a few articles on Services, however i believe this also falls short for my needs as it's not a subset of Activity and so doesn't implement onKeyDown(); and unless I'm mistaken, a wake lock actually wakes and/or unlocks the phone?</p> <p>Should I give up now, or is this actually achievable?<br> (actually it must be possible as the whole point of this is that i don't have to pay £3 for an app for this one feature. lol)</p> <p>Note: Running 4.2.1<br> New to Android, but not to Java.</p> <p>Steve.</p> <p><strong>Edit:</strong> Just a thought, but if i extended Activity to my own class (and override onKeyDown), and instantiated it in a Service as a static instance, would that custom activity persist while the phone is locked?</p> <p><strong>Edit2:</strong> I found <a href="https://stackoverflow.com/questions/3434325/is-there-a-way-to-make-an-android-app-respond-to-hardware-buttons-while-phone-lo?rq=1">this SO post</a> which suggests using FLAG_SHOW_WHEN_LOCKED. I'll start looking at this when I get home, but I'm still open to suggestions and advice :D</p> <p><strong>Edit 3:</strong> OK so tell me if i start losing the plot here...<br> Using a broadcast receiver i will listen for the ACTION_SCREEN_OFF flag. If that gets called, create my custom Activity (with the onKeyDown() Override) and set it to FLAG_SHOW_WHEN_LOCKED to take over my lock screen (i don't care because the screen is off). The receiver then listens for the ACTION_SCREEN_OFF, if called it will then destroy the Activity before showing the screen (or after, I'm not bothered if it flickers). (possibly even just pause it, and only kill it if it detects an unlock). </p> <p>Also a big thanks to @StoneBird for getting me this far, sometimes it helps to just hash it out with someone who knows what there talking about (hopefully ^_-).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload