Change volume key function in android app - java

I have source code for SmartMouse android app.
I want to alter the function of volume keys with the onscreen buttons.
I have basic knowledge for C programming but don't know java.
What part should I search for in the code?
This might be a lame question but I badly need this.

You have to capture the event as mentionned here : Android - Volume Buttons used in my application
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
dispatchKeyEvent is not only called for volume keys, it will catch all the key event so you have to :
Get the event code
Check if it's what you are seeking for
Do what you want according to the event :)
The key is dispatchKeyEvent is called before any other method by the system, so you can intercept the event

Related

Listening to key press events blocks the keys from working

I'm new to android and I'm making an app that will listen for a consecutive volume up and volume down keypress and vibrate a pattern.
I've tried using dispatchKeyEvent() and it detects volume up and down fine but it blocks the user from changing the volume.
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("UP"); // I know i can use Log but this is quicker to type
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("DOWN");
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Additionally I'd like to be able to do this when the screen is off (and app in background) but aparently services can't interact with key events.
I've found very conflicting answers all over the place; an app I used before played a sound when it was in the background, screen turned off and the power button was pressed 3 times in short succession, however many questions here have answers akin to "not possible".
I wouldn't mind using power button instead of volume keys (infact both would be nice) but that seems harder to implement.
TLDR:
How to stop listeners blocking the keys they're listening.
2. How to run this in the background.
Any help would be greatly appreciated!
Edit: regarding no2 I found a way to do this with power button by registering a broadcast receiver in a service and listening to screen off and screen on intents.
overriding the onKeyDown() of Activity is easier; because it's always KeyEvent.ACTION_DOWN:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP: {
System.out.println("UP");
}
case KeyEvent.KEYCODE_VOLUME_DOWN: {
System.out.println("DOWN");
}
}
return super.onKeyDown(keyCode, event);
}
How to stop listeners blocking the keys they're listening.
As you could see from android docs, if you don't need to block your keys, just call super.dispatchKeyEvent(event); otherwise if you return true; it means that you consumed event, nobody will know about event.
How to run this in the background.
I think, there is impossible. Android has service, which could do background job, but this not way to handle system button clicks. Maybe this topic will help you.

Capturing the volume up and down button press and returning nothing

I was wondering if I am able to disable the default volume up and down key press on the side of a cell phone.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.v("key pressed", String.valueOf(event.getKeyCode()));
return false;
//return super.dispatchKeyEvent(event);
}
I am using dispatchKeyEvent to find when the user presses the volume up or down button but I am not able to stop the phone from actually turning up the volume or turning it down. I want to enable the user to fire methods in my application if they decide to use the volume up or down button. Am I able to stop the phone from performing the volume control when my application is focused?
I used this in a previous project...perhaps it is what you're looking for:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Android - Volume Buttons used in my application

libgdx in Android: how to handle escape key?

In Android. I implement an InputProcessor and:
#override public boolean keyDown(int keycode)
When I press escape, no key is checked and the app exits. How do I check Escape key?
ps: in PC/MAC ,I can check Escape directly, and it works.
This works for me:
public boolean keyDown(int keycode){
switch (keycode){
case Keys.ESCAPE:
Gdx.app.log("Monles", "ESC pressed");
break;
...
Also, my app doesn't quit when I press ESCAPE, but if yours do, this will probably fix it:
Gdx.input.setCatchBackKey(true);
First create an InputProcessor to handle the back key actions and then add it to a multiplexer so that other components still receive similar events.
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Keys.ESCAPE) || (keycode == Keys.BACK) )
// Maybe perform other operations before exiting
Gdx.app.exit();
return false;
}
};
InputMultiplexer multiplexer = new InputMultiplexer(mStage,
backProcessor);
Gdx.input.setInputProcessor(multiplexer);
And enable catching the back key.
Gdx.input.setCatchBackKey(true);
That's all there is. Good luck!

How to manage back button with multiple screens in Libgdx?

If there is some way how to manage back button in Libgdx?
for example in Andengine I have implemented this like that:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
start(MENU);
break;
...
...
}
}
}
I don't have idea how to do it here, because ApplicationListener has only create, show, render...
I tryed this:
if (Gdx.input.isButtonPressed(Keys.BACK)){
new ScreenChangeTask(MyScreen.SPLASH);
}
but it still closes my application.
FYI: I have class Controller extends Game and I use public void setScreen (Screen screen) to switch between screens.
In order to do this properly you need to tell LibGDX to catch the back key:
Gdx.input.setCatchBackKey(true);
You should do this somewhere early in the application. And set it to false when you want the user to be able to use the back key.
set
Gdx.input.setCatchBackKey(true);
then implement below code on keyUp...
#Override
public boolean keyUp(int keycode) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
game.setScreen(new MenuScreen(game)); //MenuScreen is your class Screen
break;
return false;
}
}
}
Hope this will help you

How to disable android hiding an activity on keycode_button_... events?

I'm using a rooted tabled with an USB port in host mode to read the value of an xbox-controller-joystick (with onJoystickMotion).
Now, I also want to track the buttons keycode_button_a, keycode_button_b, keycode_button_x, keycode_button_y and keycode_button_select. The tracking is working, but pressing these buttons will hide the activity.
Is there any way to disable android hiding an activity on this keycode_button_... events?
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
InputDeviceState state = getInputDeviceState(event);
if (state != null) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (state.onKeyDown(event)) {
//Do my thing
}
break;
case KeyEvent.ACTION_UP:
if (state.onKeyUp(event)) {
//Do my thing
}
break;
}
return true;
}
return super.dispatchKeyEvent(event);
}
Yeah :D

Categories

Resources