按下主页按钮时的调用方法
我的 Android 活动之一中有此方法:
@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 有效,日志方法也永远不会触发。但这适用于后退按钮。有谁知道这是为什么以及如何让它发挥作用?
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);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Home 按钮是一个非常危险的覆盖按钮,因此,Android 不会让您像覆盖 BACK 按钮一样覆盖其行为。
看看这个讨论。< /a>
您会注意到主页按钮似乎是作为意图调用来实现的,因此您最终必须向您的活动添加一个意图类别。然后,每当用户点击主页时,您的应用程序都会显示为一个选项。您应该考虑您希望通过主页按钮实现什么目的。如果它不是要替换设备的默认主屏幕,我会担心 HOME 按钮超载,但这是可能的(根据上面线程中的讨论)。
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.
Take a look at this discussion.
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.)
我花了将近一个月的时间才解决这个问题。刚才我解决了这个问题。
在您的活动的 onPause() 中,您必须包含以下 if 条件:
函数 isFinishing() 返回一个布尔值。如果您的应用程序实际上正在关闭,则为 True;如果您的应用程序仍在运行,但例如屏幕关闭,则为 False。
希望有帮助!
It took me almost a month to get through this. Just now I solved this issue.
In your activity's onPause() you have to include the following if condition:
The function isFinishing() returns a boolean. True if your App is actually closing, False if your app is still running but for example the screen turns off.
Hope it helps!
HOME按钮不能被应用程序拦截。这是 Android 中的设计行为。原因是为了防止恶意应用程序获得对您手机的控制(如果用户无法按返回或主页,他可能永远无法退出该应用程序)。
“主页”按钮被视为用户的“安全区”,并且始终会启动用户配置的主页应用程序。
上述唯一的例外是配置为家庭替代的任何应用程序。这意味着它在相关 Activity 的 AndroidManifest.xml 中声明了以下内容:
按下主页按钮时,将调用当前主页应用的 Activity 的
onNewIntent
。The HOME button cannot be intercepted by applications. This is a by-design behavior in Android. The reason is to prevent malicious apps from gaining control over your phone (If the user cannot press back or home, he might never be able to exit the app).
The Home button is considered the user's "safe zone" and will always launch the user's configured home app.
The only exception to the above is any app configured as home replacement. Which means it has the following declared in its AndroidManifest.xml for the relevant activity:
When pressing the home button, the current home app's activity's
onNewIntent
will be called.我发现当我按下HOME按钮时,onStop()方法被调用。您可以使用下面的代码来监控它:
I found that when I press the button HOME the onStop() method is called.You can use the following piece of code to monitor it:
我有一个处理主页按钮按下的简单解决方案。这是我的代码,它可能很有用:
作为摘要,在活动中放置一个布尔值,当发生活动切换(startactivity 事件)时,设置变量并在 onpause 事件中检查此变量。
I have a simple solution on handling home button press. Here is my code, it can be useful:
As a summary, put a boolean in the activity, when activity switch occurs(startactivity event), set the variable and in onpause event check this variable..
KeyEvent.KEYCODE_HOME
无法被拦截。如果可以的话那就太糟糕了。
(编辑):我刚刚看到尼克斯的回答,非常完整;)
KeyEvent.KEYCODE_HOME
can NOT be intercepted.It would be quite bad if it would be possible.
(Edit): I just see Nicks answer, which is perfectly complete ;)
使用广播接收器
Using BroadcastReceiver
使用
onPause()
方法在主页按钮上执行您想要执行的操作。use
onPause()
method to do what you want to do on home button.我也曾因 HOME 键的使用而苦恼了一段时间。
当用户单击“主页”按钮时,我想停止/跳过后台服务(轮询位置)。
这是我实现的“类似黑客”的解决方案;
使用每个活动的
,后台服务在每个循环中检查appstate,跳过该操作
它对我来说效果很好,至少不再耗尽电池,
希望这有帮助......
I also struggled with HOME button for awhile.
I wanted to stop/skip a background service (which polls location) when user clicks HOME button.
here is what I implemented as "hack-like" solution;
on each activity
and the background service checks the appstate in each loop, skips the action
it works well for me, at least not draining the battery anymore,
hope this helps....
像这样定义
activity
中的变量:像这样定义广播接收器类:
在
onCreate
方法或onResume
方法上注册接收器,如下所示:add清单中的接收者如下所示:
define the variables in your
activity
like this:define your broadcast receiver class like this:
register reciver on
onCreate
method oronResume
method like this:add receiver in your manifest like this: