在 javascript 中捕获 Android 手机中的本机按钮点击

发布于 2024-09-16 20:20:34 字数 169 浏览 4 评论 0原文

有没有一种方法可以让我们使用phonegap/jqtouch/javascript捕获android应用程序中html文件中HOME和BACK按钮的点击?

我有一个使用phonegap 的Android 应用程序。我想捕获html页面中Android手机原生HOME和BACK按钮的点击,以优雅地退出/返回。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2024-09-23 20:20:35

您可以在 PhoneGap 中捕获 BACK 按钮事件,但不能捕获 HOME 按钮(这是一种不好的 Android 做法,因为无论您使用的应用程序如何,用户对 HOME 键的作用都有一个明确的期望:将您带回您的家屏幕!您不想覆盖此功能)。

我将引导您查看 PhoneGap 中的代码片段(最新源!从 github 获取最新版本的 PhoneGap 框架)以获取指导。

首先,有一个“BrowserKey”java对象绑定到“BackButton”JavaScript全局:

http://github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/DroidGap.java#L291

的定义这个类在这里: http:// github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/BrowserKey.java

您需要在应用程序中做的第一件事(我建议您在应用程序初始化期间运行它)是让框架的本机端知道您正在覆盖“后退”按钮功能。您可以在 JavaScript 中通过一个简单的调用来完成此操作:

BackButton.override();

从那时起,您可以将事件处理程序附加到文档的“backKeyDown”事件,以便在每次单击“后退”按钮时执行逻辑。像这样的东西应该可以工作:

document.addEventListener('backKeyDown', function(e) {
  alert('you hit the back key!');
}, false);

作为附录,这里是包装后退按钮事件调度的 JavaScript 代码: http://github.com/phonegap/phonegap-android/blob/master/framework/assets/js/keyevent.js

基本上,在调用 BackButton.override 之后(),每次按下 BACK 按钮时,框架的本机端都会调用 window.keyEvent.backTrigger()。

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).

I will direct you to pieces of code in PhoneGap (LATEST source! pull from github for latest version of the phonegap framework) for guidance.

First, there is a 'BrowserKey' java object bound to the 'BackButton' JavaScript global:

http://github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/DroidGap.java#L291

The definition of this class is here: 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:

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:

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

Basically, after calling BackButton.override(), the native side of the framework will call window.keyEvent.backTrigger() every time the BACK button is hit.

南汐寒笙箫 2024-09-23 20:20:35

此代码示例适用于 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);  

无法处理主页按钮。这是系统保留的。

This code sample works for PhoneGap 0.9.5 and later (tested on 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.

心如狂蝶 2024-09-23 20:20:35

我有一个使用phonegap 的Android 应用程序。我想在html页面中捕获Android手机本机HOME和BACK按钮的点击,以优雅地退出/返回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文