I want to detect long presses in volume key in a service. Here are my options:
A) Let the user control volume from the lock screen
I wan't to detect if the user has held down the volume button in a service while the screen is off. I already tried (for 2 days) doing it with contentObserver, but the problem is that contentObserver detects volume changes, and volume doesn't change when the screen is off. Is there any way I can let the user control volume from the lock screen?
B) Detect long press for volume button from service
How can I do this? Are there any broadcast receiver's that I can use while the screen is off? Is there a way to implement the dispatchKeyEvent in a service?
I have seen this, but for me the second answer doesn't work in the background. I think the easiest way would be option A because I already have everything set up for when the user changes volume, so can I let the user control volume from lock screen? If not, is there anything else I can do?
Thanks so much,
Ruchir
Related
on one screen in my application, I am recording audio.
Scenario: User records audio on this screen. A push comes from our application. The user clicks on this push and another push processing activity is launched.
Result: The user loses his session.
Question: what are the options for handling this case so that the user does not lose his session? Ideally, before switching to a new activity, the application would ask the user if he is sure that he wants to go to another screen, because the session will be lost. But how to track it?
Thanks a lot for your advice
The best solution I have found is to clear all old push notifications in the notification shade when going to the recording screen.
val notificationsManager = getSystemService<Any>(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificationsManager!!.cancelAll()
Further during the recording, if a push notification arrives, I receive this in my service inherited from FirebaseMessagingService in onMessageReceived().
If the recording screen is currently open, I do not show a notification in the notification shade. But I show a widget on the recording screen with the push text and if the user presses it I ask if the user wants to go to another screen because the recording will end.
I hope my experience helps someone. Similarly implemented in Intercom
I have an activity that shows up when the phone screen goes to sleep/turns off ie turns black.
For some reason, the phone turns on when the volume buttons or the camera buttons are pressed. By turns on, I mean the screen wakes up or comes back from the black screen state.
I've tried using dispatchKeyEvent(KeyEvent event) and the buttons are disabled on the activity, but they still wake up the phone.
You could try overriding the onKeyDown(KeyEvent) method and change what happens for those keys. However, I'm not too optimistic as if you're running an activity, it will be in an inactive state when the display is off, and also it could be that the phone is hard wired to wake up on those buttons. It could be device specific. Hard to say. Try that out and let me know how it goes, I'm currious
I am trying to develop an application in which I want to know when the camera button(if the phone has one),has been pressed for feature phones like Series60 using J2ME.I dont want to take a snapshot or take a video.I just want to know when the hardware button has been pressed.
I have been unable to find a proper way for this.Your help is greatly appreciated.
Create a canvas, listen for keypresses and output all events to the screen. If no event occurs when you press the camera key then you can't do it.
How can a long key press (volume keys) be detected in Android when the screen is off? Is there any way to do this?
This is not possible, sorry. The only thing that could possibly detect a long press of a volume button is a foreground activity. This also assumes that the device has hardware volume buttons, and not all devices do.
I'm trying to make an application where the user can override the default behaviour of the volume up/down buttons (as well as the screen on/off button - is this possible?). Anyways, using some code along the lines of the following I can do this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
//this is where I can do my stuff
return true; //because I handled the event
}
return false; //otherwise the system can handle it
}
But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.
Thanks for any help.
as well as the screen on/off button - is this possible?
Fortunately, no.
But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.
This is not possible for the volume buttons.
For example, AndroSS allows you to override the camera hardware button to take a screenshot.
That is the camera button. If the foreground activity does not consume a camera button click, that gets turned into a broadcast Intent, which other applications can listen for. This behavior is unique to the camera and media buttons and is not used with any other hardware buttons.
this isn't a very good idea. this means every time you press the volume up/down keys you're going to capture it regardless of where you are. what if i want to change my ringer volume? media volume? app that uses the rockers for other uses? it just won't work well and just aggravate people. plus, i'm not sure if its doable.
This is not (easily) possible, because the "onKeyDown" method is overriding Activity.onKeyDown(). For this reason, you have to have a foreground activity to receive these function calls.
I don't have any experience doing this, but you would have to dig a bit deeper into writing your own hardware key intercept/handler functions, since you wouldn't be able to access the one from the activity class.
This probably won't do what you want, but if you make an IME, the IME can capture the volume +/- buttons, and do whatever it wants, like remap to other button presses. For instance, I have modified a Gingerbread-style IME to turn volume +/- buttons to do page up/down in the Kindle and Overdrive apps.