Home键点击事件处理的android事件、Home、android

2023-09-04 13:04:59 作者:听说我们分手了╰+

我想叫某些功能显示在homebuttonclick事件pressing通知。

I wanted to call certain function for showing notifications on pressing of homebuttonclick event.

我refered 这的博客。

I refered this blog.

写code为:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_HOME){
            APP_STATUS="SLEEP";

          LocalToNotification();

        }
        return true;
    }   

但不幸的是它不工作。

But unfortunatly it was not working.

我通过调试器进行测试,但观察到调试器也没有正在添加这条线code在pressing HOME键。

I tested through debugger, but observed that debugger is also not comming on this line of code upon pressing home button.

什么是错在上述code?

What is wrong in above code?

请帮我。

推荐答案

在较旧的Andr​​oid版本,这是工作。但是Android改变了这一点,因为他们说:主页按钮应留在家按钮,他们不希望任何人覆盖主页按钮。因为这个原因,你的code不工作了。

On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.

如果你想要做的事情时,home键是pressed,然后为此在的onPause方法。

If you want to do something when the home button is pressed, then do this in the onPause method.