按下主页按钮时的呼叫方法 [英] Call method when home button pressed

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

问题描述

我的一项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有效,log方法也永远不会触发.但是,这适用于后退按钮.有谁知道这是为什么以及如何使它工作?

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?

推荐答案

主页"按钮是一个非常危险的按钮,因此,Android不允许您以与返回"按钮相同的方式覆盖其行为.

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

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

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