当home键pressed在Android上调用方法 [英] Call method when home button pressed on android

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

问题描述

所以,我在我的Andr​​oid的活动之一,有这样的方法:

So 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);
}

不过,即使KEY code_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.

看看这个<一href="http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice">discussion.

您会发现,home键似乎被实现为目的的调用,所以你最终不得不意向类别添加到您的活动。然后,用户点击家里任何时候,你的应用程序将显示为一个选项。你应该考虑什么是你正在寻找完成与home键。如果不更换设备的默认主屏幕,我会警惕超载的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键pressed在Android上调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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