简体   繁体   English

手机重启后恢复计步器数据

[英]Restore Step Counter data after phone reboot

I am writing an android app in java which uses Android phone's Step Counter sensor event values to record walking steps taken by the user.我正在 java 中编写一个 android 应用程序,它使用 Android 手机的计步器传感器事件值来记录用户的步行步数。 Basically my java code to display current Steps is as follows:基本上我的 java 代码显示当前步骤如下:

if (running) {
   totalSteps = event.values[0];
   currentSteps = totalSteps - previousTotalSteps;
   tv_stepsTaken.setText(String.valueOf(currentSteps));
}

I am using SharedPreferences to load, save and reset previousTotalSteps on a daily basis.我每天都在使用 SharedPreferences 来加载、保存和重置 previousTotalSteps。

My problem is that when phone reboots, the totalSteps event value from Step Counter Sensor is reset automatically to zero, hence it turns my currentSteps value to negative as previousTotalSteps value is then being subtracted from zero.我的问题是,当手机重新启动时,来自 Step Counter Sensor 的totalSteps事件值会自动重置为零,因此它将我的currentSteps值变为负数,因为然后从零中减去previousTotalSteps值。 Only solution that is coming to my mind is to set previousTotalSteps to zero too.我想到的唯一解决方案是将 previousTotalSteps 也设置为零。 But it will defeat the purpose of recording daily steps.但它会破坏记录每日步数的目的。 If the user reboots the phone in the middle of the day, then half day's data will be lost or otherwise turn negative in display.如果用户在一天中重新启动手机,那么半天的数据将丢失或以其他方式显示为负数。 I am also recording this daily steps value to sqlite database as daily history to be shown graphically to user.我还将这个每日步数值记录到 sqlite 数据库作为每日历史记录,以图形方式显示给用户。 So, this negative value goes to sqlite databse too and ruins the historical graph.因此,这个负值也会进入 sqlite 数据库并破坏历史图表。 I need suggestions to solve this problem.我需要解决这个问题的建议。 My reset data code is as follows:我的重置数据代码如下:

previousTotalSteps = totalSteps;
tv_stepsTaken.setText("0");
saveStepsData();
return true;

So, I have solved this problem by myself.所以,我自己解决了这个问题。 I am posting this solution for anyone else who find themselves in this situation, so that it might help them solve it.我正在为发现自己处于这种情况的任何其他人发布此解决方案,以便它可以帮助他们解决它。

For this, currentSteps as well as previousSteps need to be saved to SharedPreferences after each step .为此, currentSteps以及previousSteps需要在每个 step之后保存到 SharedPreferences 。 Earlier only previousSteps was being saved and that too once daily.之前只保存了previousSteps ,而且每天保存一次。

Thus, totalSteps-previousSteps will give an increment of one step to be added to currentSteps.因此, totalSteps-previousSteps将增加一个步长以添加到 currentSteps。

In case, the phone reboots, totalSteps value is rest to zero.万一手机重启,totalSteps 值是 rest 为零。 If that happens(or by any chance, if totalSteps is less than previousSteps), then the previousSteps value will also be set equal to totalSteps for that condition before applying both variables for current steps calculation.如果发生这种情况(或任何机会,如果 totalSteps 小于 previousSteps),则在将两个变量应用于当前步数计算之前,previousSteps 值也将设置为等于该条件的 totalSteps。

 if (running) {

            totalSteps = event.values[0];
            loadCurrentStepsData();
            loadPreviousStepsData();
            if (totalSteps == 0 || totalSteps<previousSteps) {
                previousSteps = totalSteps;
                savePreviousStepsData();
            }
            currentSteps = currentSteps + (totalSteps - previousSteps);
            previousSteps = totalSteps;
            saveCurrentStepsData();
            savePreviousStepsData();
        }
        tv_stepsTaken.setText(String.valueOf(currentSteps));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM