我非常接近完成我的项目,该项目与计算步数,覆盖距离和计算速度有关。我正在使用TYPE_COUNTER_STEP传感器来计算步长,并使用公式来计算距离,即男人乘0.415,女性乘高度0.41来获得脚长。到目前为止,一切正常,但是我很想计算速度。我不知道如何计算,我的意思是我知道s = vt,但是我需要时间来获得速度/步长。任何人都知道如何实现这一目标。这是我项目的最后一部分。我也在互联网上寻找它,但是我没有工作方法来计算时间。有人建议我使用时间戳,但我使用了时间戳,但不幸的是我得到的是无穷大文本而不是速度
我无法使用GPS来计算速度,因为GPS一直都无法使用,例如在隧道或地下等内部
谢谢
package com.lijianzhong13354146.coeur1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.DateUtils;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class AccurateStepCounter extends AppCompatActivity {
private long timestamp;
private TextView textViewStepCounter;
private TextView textViewStepDetector;
private Thread detectorTimeStampUpdaterThread;
private Handler handler;
Toolbar toolbar ;
private Button buttonStop ;
TextView stepValue ;
TextView distanceValue ;
TextView distanceTitle ;
int getCurrentStepValue ;
int actualStepTaken ;
SharedPreferences sharedPreferences ;
SharedPreferences.Editor editor ;
SharedPreferences sharedPreferencesFromSetting ;
SharedPreferences.Editor editorFromSetting ;
private boolean isRunning = true;
ArrayList<Step> arrayList ;
String gettingPersonHeight ;
double gettingPersonHeightInDoule ;
double step_length ;
SharedPreferences getSharedPreferencesHeight ;
SharedPreferences.Editor getEditoHeight ;
double timeInSecond ;
SharedPreferences sharedPreferencesGetGender ;
SharedPreferences.Editor editorGetGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accurate_step_counter);
buttonStop = (Button) findViewById(R.id.buttonSTOP);
toolbar = (Toolbar) findViewById(R.id.toolbar);
stepValue = (TextView) findViewById(R.id.step_value);
distanceValue = (TextView) findViewById(R.id.distance_value);
arrayList = new ArrayList<>();
sharedPreferences = getSharedPreferences("saveInformation" , MODE_PRIVATE) ;
sharedPreferencesFromSetting = getSharedPreferences("EmailSave" , MODE_PRIVATE);
sharedPreferencesGetGender = getSharedPreferences("saveInformation" , MODE_PRIVATE);
getSharedPreferencesHeight = getSharedPreferences("Height" , MODE_PRIVATE);
distanceTitle = (TextView) findViewById(R.id.distance_title);
gettingPersonHeightInDoule = Double.parseDouble(
getSharedPreferencesHeight.getString("saveHeight" , "0"))/100;
if (sharedPreferencesGetGender.getString("GENDER" , null).equals("Male") ){
step_length = 0.415 * gettingPersonHeightInDoule ;
}else {
step_length = 0.413 * gettingPersonHeightInDoule ;
}
ToolbarConfig();
MediaPlayer mediaPlayer1 = MediaPlayer.create(this , R.raw.boom);
mediaPlayer1.start();
registerForSensorEvents();
onStopButtonClick();
// setupDetectorTimestampUpdaterThread();
}
@Override
public void onBackPressed() {
}
public void onStopButtonClick () {
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int actualSteps = getCurrentStepValue - arrayList.get(0);
editor = sharedPreferences.edit() ;
editor.putString("STEPS" , String.valueOf(actualSteps));
editor.commit() ;
startActivity(new Intent(AccurateStepCounter.this , FinalResultDisplay.class));
}
});
}
public void ToolbarConfig () {
toolbar.setTitle(getResources().getString(R.string.app_title));
toolbar.setSubtitle("Pedometer");
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setSubtitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
}
public void registerForSensorEvents() {
SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// Step Counter
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Step step = new Step(event.values[0], System.currentTimeMillis());
arrayList.add(step);
getCurrentStepValue = (int) step.getStep();
stepValue.setText(getCurrentStepValue - arrayList.get(0) + "");
double distaneCovered = step_length * (getCurrentStepValue - arrayList.get(0));
// the elapsed time beetween 2 step will be:
if (arrayList.size() > 2) {
Step lastStep = arrayList.get(arrayList.size() - 1);
Step previousStep = arrayList.get(arrayList.size() - 2);
long msElapsedTime = lastStep.getTimestamp() - previousStep.getTimestamp();
timeInSecond = TimeUnit.MILLISECONDS.toSeconds(msElapsedTime);
}
double getVelocity = distaneCovered / timeInSecond ;
// distanceTitle shows the speed for now
distanceTitle.setText(getVelocity + " m/s");
distanceValue.setText("" + distaneCovered);
/* float steps = event.values[0];
arrayList.add((int)steps);
getCurrentStepValue = (int) steps ;
double distanceCovered = (step_length * (getCurrentStepValue - arrayList.get(0))) /1000;
stepValue.setText(getCurrentStepValue - arrayList.get(0) + "");
distanceValue.setText("" + String.format("%.2f" , distanceCovered));*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
SensorManager.SENSOR_DELAY_FASTEST);
}
}
class Step {
private float step;
private long timestamp;
Step (float step , long timestamp) {
this.step = step;
this.timestamp = timestamp ;
}
public void setStep(float step) {
this.step = step;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public float getStep() {
return step;
}
public long getTimestamp() {
return timestamp;
}
// getters
// setters
}
答案 0 :(得分:0)
您可以计算两个步骤之间的经过时间,以创建一个包含时间戳记作为字段的Step类。然后减去最后一个时间戳!
public class Step {
private float step;
private long timestamp;
// constructor
// getters
// setters
}
@Override
public void onSensorChanged(SensorEvent event) {
Step step = new Step(event.values[0], System.currentTimeMillis());
arrayList.add(step);
getCurrentStepValue = (int) step.getStep();
stepValue.setText(getCurrentStepValue - arrayList.get(0) + "");
double distaneCovered = step_length * (getCurrentStepValue - arrayList.get(0));
// the elapsed time beetween 2 step will be:
if (arrayList.size() > 2) {
Step lastStep = arrayList.get(arrayList.size() - 1);
Step previousStep = arrayList.get(arrayList.size() - 2);
long msElapsedTime = lastStep.getTimestamp() - previousStep.getTimestamp();
}
distanceValue.setText("" + distaneCovered);
}
所以您可以进行s = v / t