I am trying to build an Android TV application using Android Studio that supports channel up/down buttons as an extra features to navigate to different YouTube channels/shows/videos (like a normal satellite TV would do) rather than only supporting the D-pad navigation buttons. Is it possible to achieve this & hopefully anyone had an idea how to do this?
Thank you.
While waiting for someone to reply to this question, I've discovered the solution myself.
Basically you just need to Override onKeyDown() or onKeyUp() method in related Activity class, and don't forget to return super.onKeyDown() or super.onKeyUp() so unattended KeyEvent will be attended as normally it will.
As for my case, I'm trying to have custom features for channel up & down buttons only. So below is the sample code.
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event){
boolean handled = false;
if(KeyCode == KeyEvent.KEYCODE_CHANNEL_DOWN){
Log.i("KeyEvent","Channel down button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
else if(KeyCode == KeyEvent.KEYCODE_CHANNEL_UP){
Log.i("KeyEvent","Channel up button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
if(handled){
return handled;
}
//return super.onKeyDown() to attend unattended KeyEvent
else{
return super.onKeyDown(KeyCode, event);
}
}
KeyEvent class documentation that contains list of KEYCODE constants available can be found here.
Related
I am trying to develop a launcher TV app in android Java. I want the launcher app to show my own custom setting class when settings button is clicked from Dpad. for now when i click the settings button from dpad android system settings is triggered and displayed.
I think this can be achieved because in all the launcher of app of Android Television APP when the settings button is clicked from Dpad their custom setting class is opened rather than system settings.
Can anyone help me how can i acheive that ?
Have you tried overriding onKeyDown?
if not. you can start there.
//Handling remote control actions
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KEYCODE_SETTINGS){// from developers page
// your coder here
// and return true if you don't want system settings to be launched
return true;
}
}
But as I found out on my TV. some keys don't invoke KeyEvent. instead (in mine it was the volume keys) broadcast an action. if that is the case, you have to use broadcastReceiver and get the action from there.
for KeyEvents I ended up having a class that implements KeyEvent.Callback{}
My name is Bruno.
Unfortunately I am having a problem with key pressing.
The problem:
When I press any key from the keyboard and keep it pressed, the function onKeyUp() is called
without releasing that key. The correct behavior is: when we press a key, the function onKeyDown() is called and when that key is released, onKeyUp() is called.
I tested some Android emulators and only some of them this problem occurs. I want to use Android Studio Emulator. For example, Bluestacks this problem don't happens. I don't know why this occurs on some emulators.
Here is my code, MainActivity.java:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyUp(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyDown(keyCode, event);
}
When I press 'D' key and keep it pressed, then log prints:
log image
What is happening? There a problem with certain emulators in combination with portuguese keyboard?
Don't use onKeyUp and onKeyDown for letters. Most keyboards don't use them, they send data via the commitText() function of the InputMethodConnection. That completely bypasses the key up/key down system. Those are only reliable for physical buttons. Like the volume buttons, or a hardware keyboard attached via bluetooth.
So first off, I got this snippet of code working in an empty activity, it just calls for every time when I touch the device.
#Override
public boolean onTouchEvent(MotionEvent ev) {
System.out.println("touch");
return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
But as soon as I apply this snippet of code to an activity which also has a GridViewPager, the listener doesn't report any touch events.
So my guess is, that all touch Events are getting blocked by the GridViewPager and my listener is not getting anything.
Is there a way how I can fix that?
Hi My requirement is like this. I have an activity in that I want to disable back button, menu button, home button screen settings button. When I touch on that screen I open one alert dialog with two buttons. If I click ok button all the buttons are enabled and my app is exit.
Thats my requirement. If anyone knows about this, please reply me as soon as possible. Quick reply is Very very appreciable.
To achieve this you must specify your activity as android.category.HOME.
And when you finish your activity (press ok button) you must start default launcher on phone.
But it's mean that on every home button press you will see your activity. To prevent this you must determine how your activity was started.
I would rather override onKeyDown method
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
//DO NOTHING
}if(keyCode == ..etc){
//Still do nothing
return super.onKeyDown(keyCode, event);
}
This way you will have full control of what happens.
Regards,
Robert
I want to get my program to unhide main window when user presses some shortcut. Is there a way to get the global key events, not only the ones which happened when focus was inside application frame?
This might do what you want. Note that this code is checking for a Ctr-F keystroke. I use this code to open up a find dialog from anything in the application. I'm pretty sure that the app has to have focus though. Something to try at least...
AWTEventListener listener = new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
try {
KeyEvent evt = (KeyEvent)event;
if(evt.getID() == KeyEvent.KEY_PRESSED && evt.getModifiers() == KeyEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_F) {
}
}
catch(Exception e) {
e.printStackTrace();
}
}
};
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
EDIT: I think I understand what you want. Basically when the app does NOT have focus. If so then you'll probably have to hook into the OS events with a native API (JNI) but that forces you to a specific OS...
This might be useful. I'm not sure if there is one library that will work for Windows/Linux/Mac. For Windows you will need some external library that uses native code to create a keyboard hook. I have no idea how to do it on the other OSes.
A solution to do this by using a JFrame is to set his opacity to 0.0 and to add the Keylistener to it. But the user will see an icon in his shortcut bar...