2011-03-16 82 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處不斷積累嗎?也許你只想將分量切線整合到地球表面。