Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Phonegap加速度计不';行不通_Javascript_Cordova_Accelerometer - Fatal编程技术网

Javascript Phonegap加速度计不';行不通

Javascript Phonegap加速度计不';行不通,javascript,cordova,accelerometer,Javascript,Cordova,Accelerometer,我只是想用加速度计获取一些信息,但它就是不起作用。我的手机是三星Galaxy Y,这款手机有加速计,但当我启动我的应用程序时,它只是说“获取数据”,什么都不起作用 My index.html文件: <!DOCTYPE html> <html> <head> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale

我只是想用加速度计获取一些信息,但它就是不起作用。我的手机是三星Galaxy Y,这款手机有加速计,但当我启动我的应用程序时,它只是说“获取数据”,什么都不起作用

My index.html文件:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
    content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
<title>Accelerometer Data</title>

<script type="text/javascript" src="cordova-2.0.0.js"></script>

<!-- Add PhoneGap script here -->
<script type="text/javascript">
    // The watch id variable is set as a
    // reference to the current 'watchAcceleration'
    var watchID = null;
    // Set the event listener to run
    // when the device is ready
    document.addEventListener("deviceready", onDeviceReady, false);
    // The device is ready so let's
    // start watching the acceleration
    function onDeviceReady() {
        startWatch();
    }
    // Watch the acceleration at regular
    // intervals as set by the frequency
    function startWatch() {
        // Set the frequency of updates
        // from the acceleration
        var options = {
            frequency : 3000
        };
        // Set attributes for control buttons
        document.getElementById('startBtn').disabled = true;
        document.getElementById('stopBtn').disabled = false;
        // Assign watchAcceleration to the watchID variable
        // and pass through the options array
        watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError,
                options);
    }
    // Stop watching the acceleration
    function stopWatch() {

        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;

            var element = document.getElementById('accelerometerData');

            element.innerHTML = 'No longer watching your acceleration.'

            // Set attributes for control buttons
            document.getElementById('startBtn').disabled = false;
            document.getElementById('stopBtn').disabled = true;

        }

    }
    // Run after successful transaction
    // Let's display the accelerometer data
    function onSuccess(acceleration) {
        var element = document.getElementById('accelerometerData');
        element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />'
                + 'Acceleration Y: ' + acceleration.y + '<br />'
                + 'Acceleration Z: ' + acceleration.z + '<br />'
                + 'Timestamp: ' + acceleration.timestamp + '<br />';
    }
    // Run if we face an error
    // obtaining the accelerometer data
    function onError() {
        // Handle any errors we may face
        var element = document.getElementById('accelerometerData');
        element.innerHTML = 'Sorry, I was unable to access the acceleration data.';
    }
</script>


</head>
<body>

    <h1>Accelerometer Data</h1>

    <button id="startBtn" onclick="startWatch()">start</button>

    <button id="stopBtn" onclick="stopWatch()">stop</button>

    <div id="accelerometerData">Obtaining data...</div>

</body>
</html>

感谢您的帮助:)

我使用三星tab在cordova 2.5版上试用了您的代码,效果很好。检查您的plugins.xml是否具有以下权限,您可以发现我以前甚至没有这个文件(plugins.xml),所以我导入它并添加这一行,但仍然无法工作。我启动我的应用程序,按下“开始”按钮,什么也没发生。但当我按下“开始”按钮时,我的日志显示:“07-12 11:28:30.757:E/Web控制台(5500):Uncaught TypeError:无法在处调用未定义的方法“watchAcceleration”file:///android_asset/www/index.html:43 我只是在主线程中添加了我的主活动文件,解决了它。我在失败。只是复制并粘贴了这行代码,没有看到我应该写什么src=“cordova.js”“.对不起我的笨脑袋,谢谢你的帮助,萨利安:)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.moomob"
    android:versionCode="1"
    android:versionName="1.0" >

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <application
        android:configChanges="orientation|keyboardHidden"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="PhoneGapExamples"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="org.apache.cordova.DroidGap"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.example.moomob;

import org.apache.cordova.DroidGap;
import android.os.Bundle;

public class PhoneGapExamples extends DroidGap
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");  
    }
}