2

I am trying to capture app switch key and home key on android 3.1 and 4.0 but it doesn't seem like its working.

here is what I am doing

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
   if (KeyCode == KeyEvent.KEYCODE.KEYCODE_APP_SWITCH && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","app switch key");
    }
   else if (KeyCode == KeyEvent.KEYCODE.KEYCODE_HOME && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","home key");
    }
    //EDIT:
    return super.onKeyDown(keyCode, event);
}

My log.d statement is not printed. Is it possible to capture these 2 keys?

updated code with correct return statement

Termininja
  • 6,620
  • 12
  • 48
  • 49
Abid
  • 279
  • 2
  • 19

3 Answers3

2

Um.

Well, no, you can't.

public static final int KEYCODE_HOME

Since: API Level 1

Key code constant: Home key.

This key is handled by the framework and is never delivered to applications.

http://developer.android.com/reference/android/view/KeyEvent.html

BRPocock
  • 13,638
  • 3
  • 31
  • 50
  • how about KEYCODE_APP_SWITCH. Why this one is not captured? – Abid Dec 20 '11 at 18:13
  • I would *assume* that the Home Activity or the OS framework have trapped that key, and you cannot receive the event. It would rather defeat the purpose of having such a key, after all. – BRPocock Dec 20 '11 at 18:24
  • Its annoying...onkeydown is not even called with app_switch is pressed :( – Abid Dec 20 '11 at 18:48
1

It can be achieved using reflection.

It is under class android.os.ServiceManager having an aidl com.android.internal.statusbar.IStatusBarService and it contains toggleRecentApps method

And also try this method here

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

The super.onKeyDown(keyCode, event) is missing, i think you should try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);

   if (KeyCode == KeyEvent.KEYCODE.KEYCODE_APP_SWITCH && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","app switch key");
    }
   else if (KeyCode == KeyEvent.KEYCODE.KEYCODE_HOME && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","home key");
    }
    return true;
}

Doing the same using switch statement:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
        switch(keyCode)
        {
            case KeyEvent.KEYCODE_CAMERA:
             Toast.makeText(ListViewActivity.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
                return true;
            case KeyEvent.KEYCODE_1:
             Toast.makeText(ListViewActivity.this, "Pressed 1", Toast.LENGTH_SHORT).show();
                return true;
            case KeyEvent.KEYCODE_HOME:
             Toast.makeText(ListViewActivity.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
                return true;

            case KeyEvent.KEYCODE_BACK:
             Toast.makeText(ListViewActivity.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
                finish();
                return true;
        }

        return false;
    }
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • I tried super.onKeyDown(keyCode, event). Same result. Here is something interesting when I am pressing app switch key onkeydown is not invoked. I have a breakpoint...hmm – Abid Dec 20 '11 at 18:21
  • Can you provide more sample code, you may be missing thing, i guess. – Yaqub Ahmad Dec 20 '11 at 18:24
  • No I just updated original post with correct return statement...that is my sample code – Abid Dec 20 '11 at 18:30