本文介绍了科尔多瓦Android应用程序无法访问加速度计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的科尔多瓦[3.3.1-0.1.2] Android应用程式存取navigator.accelerometer,而在Android [4.0.3]模拟器中运行时navigator.accelerometer是不确定的。

I'm attempting to access navigator.accelerometer in my Cordova [3.3.1-0.1.2] Android app, and navigator.accelerometer is undefined when running in the Android [4.0.3] emulator.

我设立科尔多瓦应用程序时遵循本教程:

I followed this tutorial when setting up the Cordova app: http://www.techariv.in/2013/08/creating-android-hello-cordova.html

我添加了下面的WWW / config.xml文件放在<窗​​口小部件>

I added the following in www/config.xml inside of <widget>:

<plugin name="Accelerometer" value="org.apache.cordova.AccelListener" />

我也试过

<plugin name="Accelerometer" value="CDVAccelerometer" />

此外,我在WWW / JS / index.js以下(它加载执行):

Additionally, I have the following in www/js/index.js (which does load an execute):

onDeviceReady: function() {
    app.receivedEvent('deviceready');

    alert(navigator.accelerometer);
    navigator.accelerometer.getCurrentAcceleration(function() {
        alert('success');
    }, function() {
        alert('fail');
    });
},

在应用程序运行时,我得到一个警报而只是说不确定。无论是回调getCurrentAcceleration()被调用。

When the app runs, I get one alert which simply says "undefined." Neither callback to getCurrentAcceleration() is called.

注意onDeviceReady()通过

Note that onDeviceReady() is called via

document.addEventListener('deviceready', this.onDeviceReady, false)

(这是标准的Hello World的应用程序无需进行任何修改,除了增加的警报和加速度计回调。

(this is the standard "Hello World" app with no modifications besides adding the alerts and the accelerometer callback.

推荐答案

添加插件记录config.xml文件是不够的,添加插件应用科尔多瓦。一些本土code应该安装和科尔多瓦或PhoneGap的CLI连接到导航仪。

Adding plugin record to config.xml is not enough for adding plugin to cordova application. Some native code should be installed and linked to navigator by cordova or phonegap cli.

下面是一个guide.

请参阅访问功能部分。

正如您可能会注意到,科尔多瓦CLI将处理config.xml文件修改为您服务。

As you may notice, cordova cli will handle modification of config.xml for you.

无论如何,我会用这个漂亮的建议你。它可以很容易地通过PluginID管理的PhoneGap插件,或URL它。你可以为你的插件这里多插件的

Anyway, I would recommend you using of this nice grunt plugin. It can easily manage phonegap plugins via PluginID, or url to it. You may find information for your plugin for such case here and more plugins here

这篇关于科尔多瓦Android应用程序无法访问加速度计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:47