1

I'm developing a Home Screen Launcher App for Android.
Now, if the user is already on the homescreen,
i want a custom action when the user presses the the homebutton.

I know some other launchers, that can override the homebutton,
for example Go Launcher Ex.

My code is:

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    int action = event.getAction();
    int keyCode = event.getKeyCode();

    switch (action)
    {
        case KeyEvent.ACTION_DOWN:
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_HOME:
                    break;
            }
            break;

        case KeyEvent.ACTION_UP:
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_HOME:
                    if (!event.isCanceled())
                        Log.i("TEST", "HOME");
                    break;
            }
            break;
    }

    return super.dispatchKeyEvent(event);
}

But when i press the homebutton, nothing happens..

bricklore
  • 4,125
  • 1
  • 34
  • 62

2 Answers2

1

Launcher is some kind of activity.

So I believe that you will gain focus when user is pressing the home button. One thing I can tell you for sure. This is possible!

You got setOnFocusChanged() in a View class

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

Referencing this post:

You cannot "detect home key Button press event", sorry.

However on the same post Idistic suggests this as a work around:

long userInteractionTime = 0;

@Override
public void onUserInteraction() {
    userInteractionTime = System.currentTimeMillis();
    super.onUserInteraction();
    Log.i("appname","Interaction");
}

@Override
public void onUserLeaveHint() {
    long uiDelta = (System.currentTimeMillis() - userInteractionTime);

    super.onUserLeaveHint();
    Log.i("bThere","Last User Interaction = "+uiLag);
    if (uiDelta < 100)
        Log.i("appname","Home Key Pressed");    
    else
        Log.i("appname","We are leaving, but will probably be back shortly!");  
}
Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147