我用java创建了一个简单的android步进计数器。我有步骤计数器工作,但由于某种原因,当我启动应用程序时,它开始在62000步。我没有硬编码这个值在我的代码中的任何地方,我不知道为什么会显示。下面我提供了一个屏幕截图和一段代码。有什么想法吗?
ui的外观
public class Pedometer extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private TextView count;
private boolean running;
private Button reset;
private CircularProgressBar circularProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedometer);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
count = findViewById(R.id.stepTextView);
reset = findViewById(R.id.resetSteps);
circularProgressBar = findViewById(R.id.circularBar);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count.setText("0");
}
});
}
@Override
protected void onResume() {
super.onResume();
running = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if(countSensor != null) {
sensorManager.registerListener(Pedometer.this,countSensor,SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(Pedometer.this,"Sensor not found.",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
super.onPause();
running = false;
}
@Override
protected void onStop() {
super.onStop();
running = false;
}
@Override
public void onSensorChanged(SensorEvent event) {
if(running) {
int currentSteps = (int) event.values[0];
count.setText(String.valueOf(currentSteps));
circularProgressBar.setProgressWithAnimation(currentSteps);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:padding="@dimen/box_inset_layout_padding"
tools:context=".Pedometer"
tools:deviceIds="wear">
<com.mikhaellopez.circularprogressbar.CircularProgressBar
android:id="@+id/circularBar"
android:layout_width="200dp"
android:layout_height="200dp"
app:cpb_background_progressbar_color="#FF0000"
app:cpb_round_border="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<TextView
android:id="@+id/stepTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/circularBar" />
<Button
android:id="@+id/resetSteps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Reset Counter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stepTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
暂无答案!
目前还没有任何答案,快来回答吧!