1

我只是想用加速度计获取一些信息,但它不起作用。我的手机是三星 Galaxy Y,这个型号有加速度计,但是当我启动我的应用程序时,它只是说“正在获取数据”并且没有任何作用。

我的 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>

我的安卓清单:

<?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>

PhoneGapExamples.java 文件:

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");  
    }
}

感谢任何帮助:)

4

0 回答 0