检测home键preSS的机器人 [英] Detect home button press in android
问题描述
这是我发疯了一段时间了。
This has been driving me nuts for a while now.
有什么办法可靠地检测,如果home键一直pssed在Android应用程序$ P $?
Is there any way of reliably detecting if the home button has been pressed in an android application?
如果做不到这一点,有没有告诉什么原因造成的活动进入的onPause的一条有效的途径?也就是说,我们可以发现,如果它是一个新的活动启动或pressing回引起/家。
Failing that, is there a robust way of telling what caused an activity to go into onPause? i.e Can we detect if it was caused by a new activity launching or by pressing back/home.
我看到一个建议是重写的onPause(),并调用isFinishing(),但是当pressing home键只是因为它会如果一个新的活动已经开始这样失败的区分两者,这将返回false
One suggestion I have seen is to override onPause() and call isFinishing() but this will return false when pressing the home button just as it would if a new activity is starting so this fails to distinguish between the two.
任何帮助非常AP preciated。
Any help much appreciated.
**更新**: 感谢@ Android的渴望此链接: http://nisha113a5.blogspot.com/
** Update** : Thanks to @android-hungry for this link: http://nisha113a5.blogspot.com/
Overiding下面的方法:
Overiding the following method:
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
那么下面的事件将被解雇的home键presses:
Then the following event WILL get fired for home button presses:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
}
});
我不知道是否有任何副作用这一行:
I'm not sure if there are any side effects with this line:
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
因此,它似乎是流行的看法相反,你其实可以听出来home键。令人担忧的是,你可以返回false,并有家庭什么也干不了。
So it would seem that contrary to popular belief, you can in fact listen out for the home key. Worryingly, you can return false and have the home key do nothing.
更新的: 正如预期的那样,有一些副作用影响这一点 - 它似乎嵌入视频和谷歌地图不启用该模式中可见
Update: As expected, there are some side affects with this - it seems that embedded videos and google maps are not visible with this mode enabled.
更新的: 据说这个黑客无法再正常工作为Android 4.0的起的。
Update: Supposedly this hack no longer works as of Android 4.0 onwards
推荐答案
你有没有通过这个?:的检测[家居]按钮preSS
Have you gone through this?: detect [home] button press
我不是作家,但通过它来最近
I am not the author , but came across it recently
希望它可以帮助你!
****编辑**** 此不再起作用为4.0。在这篇文章的最后注释: groups.google.com/forum/ #!话题/ Android的开发者/ trRI99-HszQ 的
****EDIT**** This no longer works as of 4.0. The last comment on this post: groups.google.com/forum/#!topic/android-developers/trRI99-HszQ
这篇关于检测home键preSS的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!