[英]My step counter does not reset the steps, even after uninstall
I use Sensor.TYPE_STEP_COUNTER I know it only resets when rebooting.我使用 Sensor.TYPE_STEP_COUNTER 我知道它只在重新启动时重置。 Is there an alternative way to reset the steps to 0 when pressing a button?
按下按钮时,是否有其他方法可以将步骤重置为 0?
Please see my code, you will find the Runactivity.class请看我的代码,你会发现 Runactivity.class
Maybe I can do it in another way which resets the steps.也许我可以用另一种方式来重置步骤。 without having me to reboot every time.
无需我每次都重新启动。
public class RunActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private TextView count;
boolean activityRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_run);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
final String naam = bundle.getString("naam");
TextView NaamView = null;
Button stopRun = (Button) findViewById(R.id.stopRun);
count = (TextView) findViewById(R.id.countView);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
NaamView = (TextView) findViewById(R.id.naamRunText);
NaamView.setText(naam);
stopRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String countValue = count.getText().toString();
Log.d("countVAL", String.valueOf(countValue));
Intent myIntent = new Intent(RunActivity.this, HomeScreenActivity.class);
Bundle bundle = new Bundle();
bundle.putString("naam", naam);
sensorManager.flush(RunActivity.this);
sensorManager.unregisterListener(RunActivity.this);
count.setText("0");
onStop();
myIntent.putExtras(bundle);
startActivity(myIntent);
}
});
}
@Override
protected void onResume() {
super.onResume();
activityRunning = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if(countSensor != null){
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
}else{
Toast.makeText(this, "Jouw apparaat heeft geen sensor!", Toast.LENGTH_LONG) .show();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if(activityRunning){
count.setText(String.valueOf(event.values[0]));
}else{
event.values[0] = 0;
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
activityRunning = false;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
protected void onDestroy() {
super.onDestroy();
}
}
When you click the reset button in the app save the current step count to SharedPreferences.当您单击应用程序中的重置按钮时,将当前步数保存到 SharedPreferences。 And you'll need a way to find out when was the last reboot because every time you reboot the saved count number gets invalid.
而且您需要一种方法来找出上次重新启动的时间,因为每次重新启动时,保存的计数都会无效。
private Integer stepsInSensor;
private Integer stepsAtReset;
void onCreate() {
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
stepsAtReset = prefs.getInt("stepsAtReset", 0);
}
public void onClick(View v) {
stepsAtReset = stepsInSensor;
if (stepsAtReset != null) {
SharedPreferences.Editor editor =
getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("stepsAtReset", stepsAtReset);
editor.commit();
}
// you can now display 0:
count.setText(String.valueOf(0));
}
@Override
public void onSensorChanged(SensorEvent event) {
if(activityRunning){
stepsInSensor = Integer.valueOf(event.values[0]);
if (stepsAtReset = null) {
stepsAtReset = stepsInSensor;
}
int stepsSinceReset = stepsInSensor - stepsAtReset;
if (stepsSinceReset < 0) {
stepsAtReset = stepsInSensor;
stepsSinceReset = 0;
}
count.setText(String.valueOf(stepsSinceReset));
}else{
event.values[0] = 0;
}
}
Nope, based on the Sensor API不,基于传感器 API
A sensor of this type returns the number of steps taken by the user since the last reboot while activated.
这种类型的传感器返回自上次激活后用户采取的步数。 The value is returned as a float (with the fractional part set to zero) and is reset to zero only on a system reboot.
该值作为浮点数返回(小数部分设置为零)并且仅在系统重新启动时重置为零。
It can only be reset when the system is rebooted它只能在系统重新启动时重置
I searched on it and tried to do it with different ways.我搜索了它并尝试用不同的方式来做。 Nothing Helped.
没有任何帮助。 Then I found the most simple way possible.
然后我找到了最简单的方法。
In onSensorChanged()
just add the counter so when ever onSensorChanged()
will be called (it will be called on every step), counter will simply count the steps then show this counter to your UI instead of showing the value of event.values[0]
在
onSensorChanged()
只需添加计数器,以便当onSensorChanged()
被调用时(它将在每一步调用),计数器将简单地计算步数,然后将此计数器显示给您的 UI,而不是显示event.values[0]
on your Reset button make the counter 0 again.在您的重置按钮上再次使计数器为 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.