I need to override the android heaset hook button, the long press causes the music player starts auntomatically and I need to avoid this.
Is it possible in Android?
I try:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
return true;
}
return false;
}
But it not works.
Thanks,
It is possible to just create your own gesture recognition to account for a long press. You can start a timer on KeyDown and then check that timer on KeyUp to see if the key up was at or lower than your long press time. Or you can use the getEventTime methods to do the same function. If your question was more specific on how to intercept the headset buttons I would recommend this article, Allowing applications to play nice(r) with each other: Handling remote control buttons.
If you need some other kind of help or some code samples let me know
Related
I want to make an Android TV app which behaves like traditional TV, when the up and down button on a remote control is pressed, TV channel is switched. I have mapped the onKeyDown listener. However, everytime I press a button, "playback control glue" shows up. I need to press the button again to trigger the listen so as to switch channel.
Playback Control Glue:
https://developer.android.com/training/tv/playback/transport-controls
Is there any way to disable the
control glue" from showing up? Since my app plays live streams, I don't want user to pause the video, and seeking is not possible.
And also, how could the listen get the keydown event immediately without going through the "control glue"?
Thanks.
P.S. I am not familiar with Android development. I am modifying the TV sample code in Kotlin.
If you want to handle the key events in the activity, and not go through the control glue, you can override dispatchKeyEvent in your activity.
Like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
|| event.geyKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
//switch channel
return true;
}
return super.dispatchKeyEvent(event);
}
In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?
If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.
If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.
I wanted to get Get Key press, Key release Events out of my Activity Screen
i.e ex When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
No. Key events are generally only delivered to the foreground activity. The two exceptions are the CAMERA button (on those few devices that have one) and the MEDIA button (found on headsets) -- those are sent as broadcast Intents if the foreground activity does not consume the event.
I don't know about the SEARCH button, but BACK button you can override.
As I know HOME button can't be override.
Maybe consider using MENU button to display option menu?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//your method
return false;
}
return false;
}
I'm a beginner at programming in general. I don't even know how to begin to search for something like this. What i want to do is make a simple text app that counts. Mainly i want to count how many fish i catch. I want to be able to use the volume rocker when the screen is locked to count how many fish i catch. I haven't started on the app yet, so i don't have any coding to post. If someone can point me in the right direction, that would be wonderful. Thanks in advanced.
This code will get started listening for the volume button presses. I imagine if you want to do it while the screen is off you'll have to acquire some kind of wakelock that lets the screen shut off but still keeps your app running.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
I've created a simple android game, based on the Lunar Lander sample, and I'm having a problem with handling key events. When the activity starts, the only keys that onKeyDown or onKeyUp get called for are the dpad up/down/left/right keys. Neither the menu, back, or dpad_center keys trigger onKey methods. However, once I've pushed one of the dpad up/down/left/right buttons, pressing the menu, back, or dpad_center keys do trigger these methods. I'm not getting any errors, or any indication of what's going wrong.
It's possible that the focus is set wrong - the activity is started from a button on screen, so it could be in touchscreen mode. If that's the case, shouldn't touching the back button get me in to the right focus mode so that I can catch the event?
I'm using the emulator from SDK-1.5r3. I have not been able to try this on a real phone yet. Here's my onKeyDown.
public boolean onKeyDown(int keyCode, KeyEvent msg)
{
Log.d(TAG, "onKeyDown: " + keyCode);
return super.onKeyDown(keyCode, msg);
}
Thanks
Matt
Is this onKeyDown in a view or in the activity?
If setContentView is called passing in a view, and that view has setFocusable(true) called on it, all key events will bypass the activity and go straight into the view.
On the other hand, if your onKeyDown is in the view, and you haven't called setContentView on the Activity and setFocusable(true) on the view, then your Activity will get the key events and not the View.
Look for those specific calls but I think you're right about it being a focus issue.
Activity's onKeyDown/onKeyUp methods not always called. Unlike them dispatchKeyEvent on Activity fired always. Move keydown/keyup logic here. Works well.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// keydown logic
return true;
}
return false;
}
insert this code
getWindow().getDecorView().setFocusable(true);
if (SDK_INT >= Build.VERSION_CODES.O) {
getWindow().getDecorView().setFocusedByDefault(true);
}