覆盖主页按钮的功能按钮、功能、主页

2023-09-14 00:03:26 作者:松昃 '

在Android的我已经能够很容易忽略后退按钮的功能,但我的应用程序,我需要重写主页按钮。例如,用户在活动A ,当他presses home键, b活动启动。我试图做到以下几点,但失败了。

In android I have been able to override the functionality of back button very easily but for my app I need to override the home button. For example the user is in Activity A, when he presses the home button, activity B is launched. I have tried to do the following but it failed.

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

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        startActivity(new Intent(this, ActivityB.class));

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

我相信它可以因为做新星启动当用户对主屏幕,他< C> $ P $ $ C $ psses home键,启动提供了用户的主屏幕一列表跳转到。我需要同样的功能。如何才能实现这一目标。

I am sure it can be done because in Nova Launcher when the user is on home screen and he presses the home button, the launcher offers the user a list of home screens to jump to. I need same kind of functionality. How can this be achieved.

问候

推荐答案

试试这个:

@Override
    protected void onUserLeaveHint() {
        if (!navigating) {
            Intent intent2 = new Intent();
            intent2.setClass(ActivityA.this, ActivityB.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            forceHome(this, intent2);
        }
        super.onUserLeaveHint();
    }

public static void forceHome(Context paramContext, Intent paramIntent) {

        if (paramIntent != null) {
            ((AlarmManager) paramContext.getSystemService(ALARM)).set(1,
                    System.currentTimeMillis(),
                    PendingIntent.getActivity(paramContext, 0, paramIntent, 0));
        }

    }

由于Android的安全功能,该活动只有5秒后启动。如果您要立即启动它。用你自己的主页启动

 
精彩推荐
图片推荐