Android Launcher - Home Button

南楼画角 提交于 2021-02-11 09:42:14

问题


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..


回答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




回答2:


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!");  
}


来源:https://stackoverflow.com/questions/15991749/android-launcher-home-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!