zoukankan      html  css  js  c++  java
  • android获取当前步数

    Androidmianifest.xml

     <uses-feature android:name="android.hardware.sensor.stepcounter"/>
     <uses-feature android:name="feature:android.hardware.sensor.stepdetector"/>

    MianActivity.java

    package com.example.bushu_chuanganqi;
    
            import android.app.Activity;
            import android.content.Context;
            import android.hardware.Sensor;
            import android.hardware.SensorEvent;
            import android.hardware.SensorEventListener;
            import android.hardware.SensorManager;
            import android.os.Bundle;
            import android.widget.TextView;
    
    public class MainActivity extends Activity {
        private TextView mStepTV;
        private SensorManager mSensorManager;
        private MySensorEventListener mListener;
        private int mStepDetector = 0;  // 自应用运行以来STEP_DETECTOR检测到的步数
        private int mStepCounter = 0;   // 自系统开机以来STEP_COUNTER检测到的步数
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mStepTV = (TextView)findViewById(R.id.tv_step);
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            mListener = new MySensorEventListener();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
                    SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            mSensorManager.unregisterListener(mListener);
        }
    
        class MySensorEventListener implements SensorEventListener {
            @Override
            public void onSensorChanged(SensorEvent event) {
                System.out.println("@@@:"+event.sensor.getType()+"--"+Sensor.TYPE_STEP_DETECTOR+"--"+Sensor.TYPE_STEP_COUNTER);
                if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
                    if (event.values[0] == 1.0f) {
                        mStepDetector++;
                    }
                } else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
                    mStepCounter = (int) event.values[0];
                }
    
                    String desc = String.format("设备检测到您当前走了%d步,自开机以来总数为%d步", mStepDetector, mStepCounter);
                mStepTV.setText(desc);
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        }
    }

    layout中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/tv_step"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="走一走,看看有什么"
            android:textSize="20sp" />
    
    </LinearLayout>
  • 相关阅读:
    Reporting Service筛选器的作用
    asp.net很好用的编辑器
    Filter函数详析
    很好用的模态弹出窗口,可自定义弹出窗口外观
    asp.net 3.5异步调用方法小结
    C#面向对象的一个简单例子,继承、接口、重写都用上了,还用到了工厂模式
    漂亮的ajax图片切换
    asp.net大文件上传组件
    jQuery中文入门指南,翻译加实例,jQuery的起点教程(转)
    DirectoryEntry的使用
  • 原文地址:https://www.cnblogs.com/837634902why/p/12174228.html
Copyright © 2011-2022 走看看