Android设备的按钮和的PhoneGap /科尔多瓦 [英] Android Device buttons and PhoneGap/Cordova

查看:146
本文介绍了Android设备的按钮和的PhoneGap /科尔多瓦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设立了Android设备按钮与我的应用程序工作。按照PG /科尔多瓦文件我应该能够只需添加一个监听器,如

I am setting up the Android Device Buttons to work with my app. According to the PG/Cordova documentation I should be able to just add an listener such as

document.addEventListener("backbutton", function() {
    console.log('Back Button Pressed.'); 
}, false);

然而,使用我的Nexus 7作为测试设备时,没有任何反应。

However, when using my Nexus 7 as a test device, nothing happens.

我也试过直接将覆盖到Java:

I have also tried adding the override directly into Java:

@Override
public void onBackPressed() {
    super.loadUrl("javascript:onBackKeyDown()");
    return;
}

但再一次,似乎什么也不做。任何人有这个问题,或者解决吗?

But once again, seems to do nothing. Anyone else having this issue or fixed?

感谢您。

推荐答案

首先,乌拉圭回合的功能写入到控制台。

First of all, ur function write into the console.

document.addEventListener("backbutton", function() {
    console.log('Back Button Pressed.'); 
}, false);

您应该将设备设置为调试工具(,你会发现下面的快速提示如何做到这一点,更多信息进入的这里

You should set your device as debugging tools (, you will find below a quick hint how to do it, for more info go here )


  • 您的手机连接到PC

  • 在您的手机:检查USB调试;
        设置=>开发选项=> USB调试

  • 在您的手机:检查来源不明;
        设置>>应用>>来源不明=真

  • 您的设备添加到ADT(在你的SDK)

  • 直接从PC到移动运行项目:
        在项目上单击右键=>运行方式=>选择您的设备

现在的后退按钮点击后退按钮pressed。将被打印在Eclipse的控制台。

NOW on Back button Click, "Back Button Pressed." will be printed in the console of Eclipse.

如果您的设备没有连接到计算机,或者如果您的设备未设置为调试工具,什么都不会改变。

If your device is not connected to the Computer or if your device is not set as debugging tools, Nothing will change.

在一般情况下使用的警示()中的执行console.log()代替,如果你在设备上进行测试,而不将其设置为调试工具。

In General case Use "alert()" in stead of "console.log()" if you are testing on the device without setting it as debugging tools.

在旁边,不使用本地Java,使用原来的文档科尔多瓦的,只需添加以下code到您的JavaScript文件:

Beside, dont use the native java, use the original documentation of Cordova, just add the following code into your javascript files:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
 // whatever you want to do
 alert('Back button Pressed');
}

那么你应该的onLoad()添加到身体标记:

then you should add "onLoad()" to the body tag:

<body onLoad="onLoad()">

这篇关于Android设备的按钮和的PhoneGap /科尔多瓦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆