Android计步(简单Demo)

作者: Jimmy_gjf | 来源:发表于2018-02-23 17:13 被阅读346次

    18.03.01

    附上Demo链接,欢迎探讨撕咬~ Pedometer

    18.02.23

    最近接了个测试各大手机厂商计步芯片的活儿,所以简单弄了个计步器的小Demo,搞搞测试。后续会把测试的结果一起发上来,造福乡亲们:我就是结果😊😊

    基本功能

    实时显示从系统StepCounter芯片获取的步数。

    如果你要做个精准的计步器,只有基本步数的读取与显示是远远不够的,还可能会涉及到:

    1. 进程保活
      应用被系统意外杀死会导致数据丢失等情况,进程再次起来的时候需要有数据恢复等操作
    2. ACC与StepCounter模式筛选
      毕竟不是所有的手机都支持StepCounter芯片,在一些古老的手机上还是需要用ACC计算步数。对于两者都支持设备,哪种方式更准确呢,这就需要有个筛选策略了
    3. 高频数据读写
      在统计数据中你会看到有的芯片会在1s内回调60多次数据,这个时候就要考虑读写性能了,回调一次就写一次sp显然不是个好主意

    工程源码

    MainActivity - UI 显示
    public class MainActivity extends AppCompatActivity {
    
        private TextView tvStep;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
        }
    
        private void init() {
            tvStep = findViewById(R.id.tv_step);
    
            register();
        }
    
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        private void register() {
            StepCounterManager.getInstance().addStepCounterObserver(new Observer() {
                @Override
                public void update(Observable o, Object arg) {
                    tvStep.setText("芯片实时获取步数: " + (float) arg );
                }
            });
    
            StepCounterManager.getInstance().register();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
    
            StepCounterManager.getInstance().clearStepObserver();
            StepCounterManager.getInstance().unRegister();
        }
    }
    
    StepCounterManager - 核心StepCounter芯片注册回调类
    public class StepCounterManager implements SensorEventListener {
    
        private static final int SENSER_TYPE_C = Sensor.TYPE_STEP_COUNTER;
    
        private static StepCounterManager instance;
    
        private final String TAG = "StepCounterManager";
    
        private SensorManager mSensorManager;
        private Sensor mStepCount;
        private StepCounterObservable mStepCounterObservable;
    
        private StepCounterManager() {
            mSensorManager = (SensorManager) GlobalConfig.getAppContext().getSystemService(Context.SENSOR_SERVICE);
    
            if (mSensorManager == null) {
                Log.i(TAG, "StepCounterManager init error");
                return;
            }
    
            mStepCount = mSensorManager.getDefaultSensor(SENSER_TYPE_C);
    
            mStepCounterObservable = new StepCounterObservable();
        }
    
        static StepCounterManager getInstance() {
            if (instance == null) {
                synchronized (StepCounterManager.class) {
                    if (instance == null) {
                        instance = new StepCounterManager();
                    }
                }
            }
    
            return instance;
        }
    
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        void register() {
            if (mStepCount == null) {
                return;
            }
    
            String info = "name = "
                    + mStepCount.getName() + ", version = " + mStepCount.getVersion() + ", vendor = " + mStepCount.getVendor()
                    + ", FifoMaxEventCount = " + mStepCount.getFifoMaxEventCount()
                    + ", FifoReservedEventCount = " + mStepCount.getFifoReservedEventCount() + ", MinDelay = "
                    + mStepCount.getMinDelay() + ", MaximumRange = " + mStepCount.getMaximumRange()
                    + ", Power = " + mStepCount.getPower()
                    + ", ReportingMode = " + mStepCount.getReportingMode() + ", Resolution = " + mStepCount.getResolution() + ", MaxDelay = " + mStepCount.getMaxDelay();
    
    
            Log.i(TAG, "芯片信息 : " + info);
    
            mSensorManager.registerListener(this, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
        }
    
        void unRegister() {
            mSensorManager.unregisterListener(this);
        }
    
        void addStepCounterObserver(Observer observer) {
            mStepCounterObservable.addObserver(observer);
        }
    
        void clearStepObserver() {
            mStepCounterObservable.deleteObservers();
        }
    
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        public void flush() {
            mSensorManager.flush(this);
        }
    
        private void setStepCount(float count) {
            mStepCounterObservable.sendChange(count);
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() != SENSER_TYPE_C) {
                return;
            }
    
            setStepCount(event.values[0]);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
        }
    }
    
    StepCounterObservable - 可被监测的StepCounter数据源
    public class StepCounterObservable extends Observable {
    
        public void sendChange(float progress) {
            setChanged();
            notifyObservers(progress);
        }
    }
    
    activity_main.xml - UI
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_step"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </LinearLayout>
    

    相关文章

      网友评论

      • 人0言:您好,有地址吗学一下
        Jimmy_gjf:贴到GitHub上啦,链接: https://github.com/flora-gjf/Pedometer
      • OlivineVip:您好!有代码地址吗
        Jimmy_gjf:贴到GitHub上啦,链接: https://github.com/flora-gjf/Pedometer

      本文标题:Android计步(简单Demo)

      本文链接:https://www.haomeiwen.com/subject/etljtftx.html