Android Phonegap - 检测主页按钮?
我有以下脚本来监视 android 后退按钮操作 -
document.addEventListener("backbutton", backKeyDown, true);
function backKeyDown() {
if($.mobile.activePage.is('#homepage')){
setTimeout( function() {navigator.app.exitApp();}, 100 );
}
else {
setTimeout( function() {$.mobile.changePage("#homepage");}, 100 );
}
}
我还想在按下物理主页按钮时关闭应用程序 - 有人知道如何检测到这一点吗?
干杯 保罗
I have the following script that monitors the android back button actions -
document.addEventListener("backbutton", backKeyDown, true);
function backKeyDown() {
if($.mobile.activePage.is('#homepage')){
setTimeout( function() {navigator.app.exitApp();}, 100 );
}
else {
setTimeout( function() {$.mobile.changePage("#homepage");}, 100 );
}
}
I would like to also close the app when the physical home button is pressed - does anyone know how to detect this?
Cheers
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能这样做,但您可以在
onPause()
方法中终止您的应用程序,而不是查看更多此处编辑:
在 onPause 中执行此操作将使您的应用程序在用户离开 Activity 时被终止,我不确定是否有办法执行该操作你想要在android虽然,也不是 受到推崇的!
您可以创建一些复杂的广播消息系统,该系统以给定的时间间隔将消息从您的活动发送到应用程序实例,如果您在应用程序实例中 X 秒内没有收到任何消息,那么您就知道该应用程序处于后台。 您可以使用一个基活动类来做到这一点
,该类在调用 onPause 之前发送消息,并在您使用的所有其他活动中扩展该活动,以便您可以实现此行为。
这肯定不是最好的选择,并且会根据实施情况消耗电池寿命,因此在实施此类事情之前,您必须真正确定您需要它。
You can't do that but you can kill your app in
onPause()
method instead see more hereEDIT:
Doing that in onPause will get your app killed whenever the user leaves the activity, I am not sure if there is a way to do what you want in android though, also is not recommended!
You can create some complex broadcast messaging system that sends messages from your activities to the application instance at a given interval of time and if you don't get any message in the application instance for x seconds then you know that the app is in background..
You can do that using a base activity class that sends messages at x seconds until onPause is called and extend that activity in all the other activities that you use so you can have this behaviour.
This is for sure not the best option that is out there and will consume battery life depending on the implementation so you have to be really sure that you need this before implementing such thing.
捕获硬件的
Home
键事件是一种不好的方法,Android 永远不会允许您这样做。为了关闭应用程序,Android 框架会自动将您的应用程序发送到后台,因此您无需关心在Home
按钮按下事件上关闭您的应用程序。Capturing the Hardware's
Home
key event is bad approach and Android will never allow you to do that. and for closing the app, android framework will automatically send your app to background so you don't need to care about closing your app onHome
button pressed event.我同意 Adil 的观点。但如果您确实想要或需要退出您的应用程序,为 PhoneGap 的“暂停”事件添加相同的侦听器/函数可能会成功。
I agree with Adil.. but if you really want to or need to exit your app, adding the same listener/function for PhoneGap's "pause" event would probably do the trick.