android Gets the current number of steps

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;   // Since the application running STEP_DETECTOR detected number of steps 
    Private  int mStepCounter = 0 ;    // since system startup to detect the number of steps STEP_COUNTER 

    @Override 
    protected  void the onCreate (the Bundle savedInstanceState) { 
        super.onCreate (savedInstanceState); 
        the setContentView ( R.layout.activity_main); 

        mStepTV = (the TextView) the findViewById (R.id.tv_step); 
        mSensorManager = (the SensorManager) the getSystemService (Context.SENSOR_SERVICE); 
        mListener = new 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++;
                }
            } the else  IF ( Event .sensor.getType () == Sensor.TYPE_STEP_COUNTER) { 
                mStepCounter = ( int ) Event .values [ 0 ]; 
            } 

                String desc = String.format ( " detecting your current away% d steps, from since the total power steps% d " , mStepDetector, mStepCounter); 
            mStepTV.setText (desc); 
        } 

        @Override 
        public  void onAccuracyChanged (the Sensor Sensor, int Accuracy) { 
        } 
    } 
}

layout in

<?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>

 

Guess you like

Origin www.cnblogs.com/837634902why/p/12174228.html
Recommended