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);
}
Related
I'm using Gluon and have an audio player. When I call the audio and play it all works normally until I press the home button. What I want it to do is to stop the music not to continue playing as it does now. Currently I've tried using an event listener to capture the event and stop the music but it's not recognising the event, I'm thinking either I have assigned the wrong keycode or it simply doesn't work like that. I have a setOnHiding method in the view already and that only works on the back button. I've also tried setOnHidden and setOnCloseRequest as well with no luck. The event listener is below.
if (event.getCode().equals(KeyCode.HOME) && KeyEvent.KEY_PRESSED == event.getEventType()) {
if (service1 != null) {
service1.backPressed();
}
Add a Listener to LifecycleEvent.Pause:
Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> stopPlayback());
The PAUSE event is fired when an application loses focus (e.g. on Android / iOS when the focus is switched out of view (but still running in the background)).
I am building an application using Xamarin and it's Android Player for Android. Whenever I hit the back button it seems to back out of the application completely. I want to be able to change this default to go back to the previous page in my app. How do I override the back button behavior?
So the previous answer is correct, you can trap the hardware back button and do whatever you want with it. But I want to make sure you understand why this is happening. Android handles the hardware back button for you, and most of the time, letting Android handle it will work, it knows what to do.
But in your case, you're not actually navigating at all. Your click handler is removing one layout file and replacing it with another. This is the equivalent of showing/hiding a div in web development. You're not actually changing the screen(page).
Because of this, when you hit the back button, you're still on the first (and only) screen in your app, so the OS does the only thing it knows to do, it closes the app.
If you want to continue with the paradigm of showing/hiding layouts in lieu of actually navigating, I would trap the hardware back button and re-swap your layout files.
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
SetContentView(Resource.Layout.Login)
return false;
}
return true;
}
But the true solution that I would recommend would be to read up on how to truly navigate in Xamarin Android. Swapping your layout files and putting all the logic for your entire app in one Activity will be very hard to maintain.
You can capture the OnKeyDown and decide whether to allow the Back button to ripple up the event chain or not:
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
Toast.MakeText (this, "Back button blocked", ToastLength.Short).Show ();
return false;
}
Toast.MakeText (this, "Button press allowed", ToastLength.Short).Show ();
return true;
}
you can handle that in the OnBackPressed() 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 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
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;
}