Android capturing volume up/down key presses in broadcast receiver? - java

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.

Related

Why sometimes my Android App restart without a logic explanation?

I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}

How to detect long presses of the volume key in a service?

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

How to listen to a combination of events in an Android Service

I have an android service (in Java) that currently invokes a function that does things like getting geolocation coordinates when the user shakes the phone repeatedly.
However, to reduce false-positives, I would like to be able to listen to a combination of events -- a hardware button press (Volume down, for instance) along with a repeated shaking, which would then invoke the same function as above.
You write a listener to a hardware key press (look at onKeyDown and onKeyUp). You write your shake detector. Have each of those set a boolean flag when its detected, and clear the flag when its over (for the shake you'd want to clear it either when the shake ends, or after some amount of time). Have each listener call getDataIfNeeded(). Have that function check if both flags are true, and if so do whatever it is you wanted to do.
You can have booleans like #gabe suggested for both events and in onReceive of both of them you can check if both booleans are true do something. also create a timer that sets those booleans to false in ever one second or so. That will keep track of both event happening together.

Is there a way to fire a method from the press of the convenience key using android?

Is it possible to have my android application start a method from pressing of the convenience key when the application in focus?
On my phone the convenience key is a button located on the right hand side of the phone. I would like to be able to know if the user presses any of the buttons on the phone, even the volume would work.
You are welcome to override onKeyDown() on your Activity and watch for a KeyEvent of interest. Note that not every key in the KeyEvent JavaDocs is accessible this way (e.g., power). Also note that it is possible that some specific View that is aware of these keys might consume the event first, though that is relatively unlikely for anything that matches your description of a "convenience key".

android-how to prevent screen from turning on in this case? [duplicate]

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

Categories

Resources