javascript 用javascript捕获Android手机中的原生按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3608669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capturing native button clicks in Android phone in javascript
提问by Newbee
Is there a way by which we can capture the click of HOME and BACK button in the html file in android application using phonegap/jqtouch/javascript?
有没有一种方法可以使用phonegap/jqtouch/javascript在android应用程序的html文件中捕获主页和返回按钮的点击?
I have an application for Android using phonegap. I want to capture the click of native HOME and BACK button of the Android phone in the html page to exit / go back gracefully.
我有一个使用 phonegap 的 Android 应用程序。我想在html页面中捕获Android手机原生HOME和BACK按钮的点击,以优雅地退出/返回。
回答by fil maj
You can catch the BACK button event in PhoneGap, however not the HOME button (this is a bad Android practice as there is a clear user expectation regardless of the app you're using about what the HOME key does: sends you back to your home screen! You don't want to override this functionality).
您可以在 PhoneGap 中捕获 BACK 按钮事件,但不能捕获 HOME 按钮(这是一种糟糕的 Android 做法,因为无论您使用的应用程序如何,用户都对 HOME 键的作用有明确的期望:将您送回您的家屏幕!您不想覆盖此功能)。
I will direct you to pieces of code in PhoneGap (LATEST source! pull from github for latest version of the phonegap framework) for guidance.
我将引导您使用 PhoneGap 中的代码片段(最新源代码!从 github 中提取最新版本的 phonegap 框架)以获得指导。
First, there is a 'BrowserKey' java object bound to the 'BackButton' JavaScript global:
首先,有一个“BrowserKey”Java 对象绑定到“BackButton”JavaScript 全局:
The definition of this class is here: http://github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/BrowserKey.java
这个类的定义在这里:http: //github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/BrowserKey.java
First thing you need to do in your application (I suggest you run this during application initialization) is to let the native side of the framework know you are overriding BACK button functionality. You would do this in JavaScript with a simple call:
您需要在您的应用程序中做的第一件事(我建议您在应用程序初始化期间运行它)是让框架的本机端知道您正在覆盖 BACK 按钮功能。你可以在 JavaScript 中通过一个简单的调用来做到这一点:
BackButton.override();
From there on out, you can attach an event handler to the document's 'backKeyDown' event to execute logic every time the BACK button is hit. Something like this should work:
从那时起,您可以将事件处理程序附加到文档的 'backKeyDown' 事件,以在每次点击 BACK 按钮时执行逻辑。这样的事情应该工作:
document.addEventListener('backKeyDown', function(e) {
alert('you hit the back key!');
}, false);
As an addendum, here is the JavaScript code that wraps the back button event dispatching: http://github.com/phonegap/phonegap-android/blob/master/framework/assets/js/keyevent.js
作为附录,这里是包装后退按钮事件调度的 JavaScript 代码:http: //github.com/phonegap/phonegap-android/blob/master/framework/assets/js/keyevent.js
Basically, after calling BackButton.override(), the native side of the framework will call window.keyEvent.backTrigger() every time the BACK button is hit.
基本上,在调用 BackButton.override() 之后,框架的本机端将在每次按下 BACK 按钮时调用 window.keyEvent.backTrigger()。
回答by Reborn
This code sample works for PhoneGap 0.9.5 and later (tested on 0.9.6) :
此代码示例适用于 PhoneGap 0.9.5 及更高版本(在 0.9.6 上测试):
document.addEventListener("menubutton", function () {
alert('Menu button');
}, false);
document.addEventListener("searchbutton", function () {
alert('Search button');
}, false);
document.addEventListener("backbutton", function () {
alert('Back button');
}, false);
Home button can't be handled. That's reserved by the system.
无法处理主页按钮。这是系统保留的。
回答by Newbee
I have an application for Android using phonegap. I want to capture the click of native HOME and BACK button of the Android phone in the html page to exit/go back gracefully.
我有一个使用 phonegap 的 Android 应用程序。我想在html页面中捕获Android手机原生HOME和BACK按钮的点击,以优雅地退出/返回。

