2011-03-16 80 views
0

我已经创建了一个使用加速度计计算设备速度的速度计应用程序。这里是代码: `package com.exacmple.speedo;速度计使用加速度计无输出

import java.util.Date; 
import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.content.Context; 
import android.hardware.SensorListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 

public class Speedometer extends Activity { 
SensorManager sensorManager; 
TextView myTextView; 
float appliedAcceleration = 0; 
float currentAcceleration = 0; 
float velocity = 0; 
Date lastUpdate; 
Handler handler = new Handler(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    myTextView = (TextView) findViewById(R.id.myTextView); 
    lastUpdate = new Date(System.currentTimeMillis()); 
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    sensorManager.registerListener(sensorListener, 
      SensorManager.SENSOR_ACCELEROMETER, 
      SensorManager.SENSOR_DELAY_FASTEST); 
    Timer updateTimer = new Timer("velocityUpdate"); 
    updateTimer.scheduleAtFixedRate(new TimerTask() { 
     public void run() { 
      updateGUI(); 
     } 
    }, 0, 1000); 
} 

private void updateGUI() { 
    // Convert from m/s to mph 
    final double mph = (Math.round(100 * velocity/1.6 * 3.6))/100; 
    // Update the GUI 
    handler.post(new Runnable() { 
     public void run() { 
      myTextView.setText(String.valueOf(mph) + "mph"); 
     } 
    }); 
} 

private void updateVelocity() { 
    // Calculate how long this acceleration has been applied. 
    Date timeNow = new Date(System.currentTimeMillis()); 
    long timeDelta = timeNow.getTime() - lastUpdate.getTime(); 
    lastUpdate.setTime(timeNow.getTime()); 
    // Calculate the change in velocity at the 
    // current acceleration since the last update. 
    float deltaVelocity = appliedAcceleration * (timeDelta/1000); 
    appliedAcceleration = currentAcceleration; 
    // Add the velocity change to the current velocity. 
    velocity += deltaVelocity; 
    // Convert from meters per second to miles per hour. 
    double mph = (Math.round(velocity/1.6 * 3.6)); 
    myTextView.setText(String.valueOf(mph) + "mph"); 
} 

private final SensorListener sensorListener = new SensorListener() { 
    double calibration; 
    public void onSensorChanged(int sensor, float[] values) { 
     double x = values[SensorManager.DATA_X]; 
     double y = values[SensorManager.DATA_Y]; 
     double z = values[SensorManager.DATA_Z]; 
     double a = -1 
       * Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) 
         + Math.pow(z, 2)); 
     if (calibration == Double.NaN) 
      calibration = a; 
     else { 
      updateVelocity(); 
      currentAcceleration = (float) a; 
     } 
    } 
    public void onAccuracyChanged(int sensor, int accuracy) { 
    } 
}; 

}` 我面临的问题是,它并没有显示在在我的情况0.0英里每小时初始速度的任何变化。我已经在logcat上检查过它,它显示了它们的速度,但不会在UI上显示任何增量。请帮忙。

回答

0

我认为你的方法使用updateGUI的处理程序是什么导致你的问题;以及您的程序循环中的时间。我试图从你的代码中构建一个你正在说的东西的原型,并且它似乎只想更新一次。然后我做了几个mod,其中一个去掉了处理程序。得到这个,当我这样做的时候,它会在模拟器标题横幅(Win7上的双显示器显示设置)上单击并保持一秒时更新textView。从来没有见过类似的东西,但我假设它是由于仿真软件中某种状态变化而启动的功能。抱歉,我无法提供更多帮助,只是让你知道你走在正确的道路上。如果我解决这个计划,我一定会发布结果。

0

只需在onSensorChanged的末尾拨打updateGUI即可。如果速度没有改变,更新UI没有意义,所以它也更有效率。

0

难道那代码只会在〜9.8m/ss处不断积累吗?也许你只想将分量切线整合到地球表面。