Stop Sound when Hold key is pressed - java

I want to stop media sound when the user press hold key or power key
I do this
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_POWER){
pauseSound();
}
return super.onKeyDown(keyCode, event);
}
But this override function didn't invoke when Power or end call is pressed
Any idea?

Thanks it solved, Ovveride EndCall & Power is forbidden for security reasons, I overrided "OnPause" (of activity) and it works good, Thanks again

Related

How to handle different keyboard action in Anrdoid app?

I have attached a physical keyboard to an Android tablet using USB. How can I call a function or perform some action in an Android app on pressing a paticular key, for example when pressing the zero key?
How could I implement the below code (for the zero key)?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
// your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
It was such a silly mistake i didnt import Keyevent ..

How to get the keystrokes from the android soft keyboard

I'm trying to authenticate users in my app by their keystrokes on the soft keyboard, I want to get:
The key release and press.
the pressure on the key.
the finger area on the key.
I used the onKeyUp and onKeyDown but It doesn"t work.
So how can I get these events from the soft keyboard.
Instead of using the onKeyDown from the view, override dispatchKeyEvent at the activity level. This will handle your key event before it gets to the window manager, so make sure to call super on any key events you do not explicitly handle. Example using the ACTION_DOWN (as every event has both ACTION_UP and ACTION_DOWN)
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_UP) {
return super.dispatchKeyEvent(event);
}
if (keyCode == KeyEvent.KEYCODE_DEL) {
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
} else {
return super.dispatchKeyEvent(event);
}
}

Android TV emulator not recognizing media key events

I have this event handler in my activity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
//do something
return true;
case KeyEvent.KEYCODE_MEDIA_REWIND:
//do something
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
While debugging in Android TV device emulator, I can see KEYCODE_DPAD_LEFT and KEYCODE_MEDIA_PLAY_PAUSE when I press a button in directional pad extended control.
But when I press "fast forward" or "rewind" media keys, the key up event is triggered, but the key code is "unrecognized".
KeyEvent { action=ACTION_UP, keyCode=KEYCODE_UNKNOWN, scanCode=208...
Is this a Google bug or am I doing something wrong here?
It seems that the TV Emulator actually does not provide the correct keycode here, which seems to be a bug.
You can simulate this via the command line:
adb shell input dpad keyevent 90
This will trigger the KEYCODE_MEDIA_FAST_FORWARD button.

How Do You Call OnVolumeDown In Other Functions?

I have:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// Do your thing
TheTimer.cancel();
TheTimer.start();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
Right now I have a countDownTimer and I want to call onKeyDown() within it. I know that I want
onKeyDown(KEYCODE_VOLUME_DOWN, ?????)
What is it I put into the second argument?
Move your code in onKeyDown() into a another fuction. And it's not ideal to manually call fuctions like onKeyDown as they called automatically by the system when that events happens.
void keyDown() {
TheTimer.cancel();
TheTimer.start();
}
Then in the onKeyDown() and inside the countDownTimer call the keyDown() method.

How does the app still work when pausing the app?

This is my code when I press the volume up button to next / prev songs :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
prevSongs();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
nextSongs();
return true;
} else return super.onKeyDown(keyCode, event);
}
But when the application exits (with the home button to return to the home screen) and I press the volume up button again, it does not work.
How does it work whether it's in or out of the app?
Thank !
You need to create a service to handle your playing media. When you exit the app, you can't listen to volume events, but in service you can do it. Also, listening to volume keys to change songs is a bad idea. You should use a notification with actions (buttons).

Categories