按下home键时的调用方法 [英] Call method when home button pressed

查看:31
本文介绍了按下home键时的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一项 Android 活动中使用了此方法:

I have this method in one of my Android Activities:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Log.d("Test", "Back button pressed!");
    }
    else if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        Log.d("Test", "Home button pressed!");
    }
    return super.onKeyDown(keyCode, event);
}

但是,即使 KEYCODE_HOME 有效,日志方法也永远不会触发.不过,这适用于后退按钮.有谁知道这是为什么以及如何让它发挥作用?

But, even though the KEYCODE_HOME is valid, the log method never fires. This works for the back button though. Does anyone know why this is and how to get this to work?

推荐答案

Home 按钮是一个非常危险的按钮,需要覆盖,因此,Android 不会让您以与执行 BACK 按钮相同的方式覆盖其行为.

The Home button is a very dangerous button to override and, because of that, Android will not let you override its behavior the same way you do the BACK button.

看看这个讨论.

您会注意到主页按钮似乎是作为意图调用实现的,因此您最终必须向活动添加意图类别.然后,每当用户回家时,您的应用程序都会显示为一个选项.您应该考虑您希望通过主页按钮完成什么.如果不替换设备的默认主屏幕,我会担心 HOME 按钮过载,但这是可能的(根据上面线程中的讨论.)

You will notice that the home button seems to be implemented as a intent invocation, so you'll end up having to add an intent category to your activity. Then, any time the user hits home, your app will show up as an option. You should consider what it is you are looking to accomplish with the home button. If its not to replace the default home screen of the device, I would be wary of overloading the HOME button, but it is possible (per discussion in above thread.)

这篇关于按下home键时的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆