-1

I am developing an application for android tv box with an IR remote . In my application I have successfully launched a web browser from my code , and I want to close this browser when the user press a button on the IR remote. I have tried to catch the key event by listening for the dispatch key event in my code , but key event is not getting caught . Could Someone please help me out in catching key press event in my application . Thank you

@Override  
public boolean dispatchKeyEvent(KeyEvent event) {

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
       if(event.getKeyCode()==170) {
       // some code  
        }
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(new ComponentName("org.mozilla.firefox", "org.mozilla.firefox.App"));
    Bundle b = new Bundle();
    b.putBoolean("new_window", true); 
    i.putExtras(b);
    i.addCategory("android.intent.category.CATEGORY_LAUNCHER");
    startActivity(i);
}
gre_gor
  • 5,546
  • 9
  • 35
  • 41
user2949215
  • 493
  • 1
  • 6
  • 11

1 Answers1

1

If you start a new Activity (i.e. Firefox) then this is the Activity on top and the one that get focus and thus the key events.

Diego Torres Milano
  • 57,580
  • 7
  • 101
  • 124
  • Could you please suggest how to get key events when the user is using firefox? thanks.. – user2949215 Dec 04 '16 at 07:26
  • @user2949215 You can't. That's part of the security model of Android- key events cannot be redirected or programatically sent to other apps/from other apps. Instead what you can do is launch your own activity with a WebView in it, rather than using the browser. – Gabe Sechan Dec 04 '16 at 07:36