安卓怎么才能一天就拿到传感器步数数据呢?

android how to get the sensor step counter data only one day?(安卓怎么才能一天就拿到传感器步数数据呢?)
本文介绍了安卓怎么才能一天就拿到传感器步数数据呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取APP用户的步数数据,但获取的数据不是当天的数据,如何才能获取唯一的当天数据?

 public StepCounterRecord(ReactApplicationContext reactContext) {
        mSensorManager = (SensorManager)reactContext.getSystemService(reactContext.SENSOR_SERVICE);
        mReactContext = reactContext;
    }

    public int start(int delay) {
    this.delay = delay;
    if ((mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)) != null) {
        mSensorManager.registerListener(this, mStepCounter, SensorManager.SENSOR_DELAY_FASTEST);
        return (1);
    }
    return (0);
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    Sensor mySensor = sensorEvent.sensor;
    WritableMap map = mArguments.createMap();

    if (mySensor.getType() == Sensor.TYPE_STEP_COUNTER) {
        long curTime = System.currentTimeMillis();
        i++;
        if ((curTime - lastUpdate) > delay) {
            i = 0;
            map.putDouble("steps", sensorEvent.values[0]);
            sendEvent("StepCounter", map);
            lastUpdate = curTime;
        }
    }
}

推荐答案

Sensor.TYPE_STEP_COUNTER返回自上次重新启动以来用户在激活时执行的步骤数。
因此,如果您想得到今天的步骤,您应该通过编程进行计算。
下面的代码是我对Calcualte It的想法

private int milestoneStep;

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    int totalStepSinceReboot = sensorEvent.values[0];

    int todayStep = getPreferences(today());
    if(todayStep == 0){
        milestoneStep = totalStepSinceReboot;
    }else{
        int additionStep = totalStepSinceReboot - milestoneStep;
        savePreferences(today(), todayStep + additionStep);
        milestoneStep = totalStepSinceReboot;

        Log.i("TAG","Your today step now is "+getPreferences(today()));
    }
}


private void savePreferences(String key, int value) {
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}

private void getPreferences(String key){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    return; = sharedPreferences.getInt(key, 0);

}

public String today() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(Calendar.getInstance().getTime());
}

这篇关于安卓怎么才能一天就拿到传感器步数数据呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)