如何每天重置计步器?
How to reset a step counter daily?
我正在创建一个计步器应用程序。几乎完成了,除了一件事。当一天过去后,我想重置步骤 numbers.How 以在我的代码中实施 sharedpreferences
?
如何重置步数?
我试过这种方式。但是当时间过去时,每个数字都变成正常值。不是从零开始。
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor == stepCounter){
stepCount = (int) sensorEvent.values[0];
////////////////
saveSteps(stepCount);
resetStep(stepCount);
////////////////
progressBar.setProgress(stepCount);
textView.setText(String.valueOf(stepCount));
txtstepinfo.setText("Adım: " + String.valueOf(stepCount) );
///////////
progressBar.setProgress(stepCount);
Log.i("sda",String.valueOf(stepCount));
/////////////////////
txtcalinfo.setText("Kalori: "+calculateCalori(stepCount));
txtDistanceinfo.setText("Mesafe: "+calculateDistance(stepCount));
}
}
private void resetStep(int s){
Calendar date = Calendar.getInstance();
if(date.get(Calendar.HOUR) == 0 && date.get(Calendar.MINUTE) == 00 ){
editor.putInt("step",0);
editor.apply();
stepCount = 0;
txtstepinfo.setText(String.valueOf(stepCount));
}
}
private void saveSteps(int s){
editor.putInt("step",s);
editor.apply();
}
希望我的回答清楚,我会更正你的代码。
if (date.get(Calendar.HOUR) == 0 && date.get(Calendar.MINUTE) == 00 ) {
editor.putInt("step",0);
editor.apply();
stepCount = 0;
txtstepinfo.setText(String.valueOf(stepCount));
}
这意味着,当应用程序在 00:00:00 - 59 秒打开时,这段代码只是 运行 好。我有一个简单的条件来重置你的共享偏好,但我没想到这是最好的答案。
val preferences by lazy { applicationContext.getSharedPreferences("KEY", MODE_PRIVATE) }
val calendar by lazy {Calendar.getInstance() }
val date by lazy { calendar.get(Calendar.DAY_OF_MONTH) } // 1 - 31
val dateKey = "DATE_NOW"
val stepKey = "STEP_COUNT"
// Check the date
if (preferences.getInt(dateKey, 0) != date) {
// Clear shared preferences
preferences.edit { clear() }
// Persistent the date
preferences.edit { putInt(dateKey, date) }
}
val myStep = preferences.getInt(stepKey, 0)
preferences.edit { putInt(stepKey, myStep + 1) }
我正在创建一个计步器应用程序。几乎完成了,除了一件事。当一天过去后,我想重置步骤 numbers.How 以在我的代码中实施 sharedpreferences
?
如何重置步数?
我试过这种方式。但是当时间过去时,每个数字都变成正常值。不是从零开始。
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor == stepCounter){
stepCount = (int) sensorEvent.values[0];
////////////////
saveSteps(stepCount);
resetStep(stepCount);
////////////////
progressBar.setProgress(stepCount);
textView.setText(String.valueOf(stepCount));
txtstepinfo.setText("Adım: " + String.valueOf(stepCount) );
///////////
progressBar.setProgress(stepCount);
Log.i("sda",String.valueOf(stepCount));
/////////////////////
txtcalinfo.setText("Kalori: "+calculateCalori(stepCount));
txtDistanceinfo.setText("Mesafe: "+calculateDistance(stepCount));
}
}
private void resetStep(int s){
Calendar date = Calendar.getInstance();
if(date.get(Calendar.HOUR) == 0 && date.get(Calendar.MINUTE) == 00 ){
editor.putInt("step",0);
editor.apply();
stepCount = 0;
txtstepinfo.setText(String.valueOf(stepCount));
}
}
private void saveSteps(int s){
editor.putInt("step",s);
editor.apply();
}
希望我的回答清楚,我会更正你的代码。
if (date.get(Calendar.HOUR) == 0 && date.get(Calendar.MINUTE) == 00 ) {
editor.putInt("step",0);
editor.apply();
stepCount = 0;
txtstepinfo.setText(String.valueOf(stepCount));
}
这意味着,当应用程序在 00:00:00 - 59 秒打开时,这段代码只是 运行 好。我有一个简单的条件来重置你的共享偏好,但我没想到这是最好的答案。
val preferences by lazy { applicationContext.getSharedPreferences("KEY", MODE_PRIVATE) }
val calendar by lazy {Calendar.getInstance() }
val date by lazy { calendar.get(Calendar.DAY_OF_MONTH) } // 1 - 31
val dateKey = "DATE_NOW"
val stepKey = "STEP_COUNT"
// Check the date
if (preferences.getInt(dateKey, 0) != date) {
// Clear shared preferences
preferences.edit { clear() }
// Persistent the date
preferences.edit { putInt(dateKey, date) }
}
val myStep = preferences.getInt(stepKey, 0)
preferences.edit { putInt(stepKey, myStep + 1) }