Android 如何将颤振计步器上的步长计数值重置为0?
我正在我的颤振应用程序上使用计步器插件()。有人能给我一个如何重置步长计数值的答案吗?已尝试使用计时器将值重置为0,但该值仍然是上一步计数的值 下面是我用来测试计步器的代码: 请帮帮我,我是新来的。谢谢Android 如何将颤振计步器上的步长计数值重置为0?,android,flutter,pedometer,Android,Flutter,Pedometer,我正在我的颤振应用程序上使用计步器插件()。有人能给我一个如何重置步长计数值的答案吗?已尝试使用计时器将值重置为0,但该值仍然是上一步计数的值 下面是我用来测试计步器的代码: 请帮帮我,我是新来的。谢谢 @override void initState() { super.initState(); setUpPedometer(); } void setUpPedometer() { Pedometer pedometer = new Pedometer()
@override
void initState() {
super.initState();
setUpPedometer();
}
void setUpPedometer() {
Pedometer pedometer = new Pedometer();
_subscription = pedometer.stepCountStream.listen(_onData,
onError: _onError, onDone: _onDone, cancelOnError: true);
}
void _onData(stepCountValue) async {
setState(() {
_stepCountValue = "$stepCountValue";
_step = stepCountValue;
});
var dist = _step;
double y = (dist + .0);
setState(() {
_numerox =
y;
});
var long3 = (_numerox);
long3 = num.parse(y.toStringAsFixed(2));
var long4 = (long3 / 10000);
int decimals = 1;
int fac = pow(10, decimals);
double d = long4;
d = (d * fac).round() / fac;
print("d: $d");
getDistanceRun(_numerox);
setState(() {
_convert = d;
print(_convert);
});
}
void reset() {
setState(() {
int stepCountValue = 0;
stepCountValue = 0;
_stepCountValue = "$stepCountValue";
});
}
void _onDone() {}
void _onError(error) {
print("Flutter Pedometer Error: $error");
}
//function to determine the distance run in kilometers using number of steps
void getDistanceRun(double _numerox) {
var distance = ((_numerox * 78) / 100000);
distance = num.parse(distance.toStringAsFixed(2)); //dos decimales
var distancekmx = distance * 1000000;//34;
distancekmx = num.parse(distancekmx.toStringAsFixed(2));
//print(distance.runtimeType);
setState(() {
_km = "$distance";
//print(_km);
});
setState(() {
_kmx = num.parse(distancekmx.toStringAsFixed(2));
});
}
//function to determine the calories burned in kilometers using number of steps
void getBurnedRun() {
setState(() {
var calories = _kmx; //dos decimales
_calories = calories==null?"0":"$calories";
//print(_calories);
});
}
通过阅读源代码,我会说当应用程序启动时,它是连续启动的。您必须自己记录计数,并记住当用户按下reset(如11230)时的计数等于0并减去该偏移量。正如@Ride Sun所建议的,您需要记录正在保存的步骤并减去该偏移量。如果你想每天重置它,你可以找到一个完整的演练我是如何做到的。这项任务也在Github中完成 大致而言,跟踪日常步骤的基本要点如下: 您将需要以下变量:
- savedStepCount以保存前几天的 步数(例如使用共享的_首选项)。如果您已经保存并 显示/使用/显示前几天的步数,不能使用该变量 为此,由于此savedStepCount将间歇性重置
- lastDaySaved,可以是int或DateTime,用于保存上次 “重置”你的计步器。这一点很快就会显现出来。坚持 这也是
- 值是从计步器流接收的计数
- 今天的步骤计数,自我描述
if(值
如何按天、周、月存储此值??
if (value < savedStepCount) {
// Upon device reboot, pedometer resets. When this happens, the saved counter must be reset as well.
savedStepCount = 0;
// {persist this value using a package of your choice here}
}
var lastDaySaved;
// {load the saved value using a package of your choice here}
if (lastDaySaved < todayDayNo) { // whether you use int or DateTime, this should only return true once every 24 hours, otherwise
your value will reset too frequently.
lastDaySaved = todayDayNo
// {save lastDaySaved here}
savedStepCount = value;
// {save savedStepCount here}
}
todayStepCount = value - savedStepCount;
return todayStepCount; // this is your daily steps value.