android 获取当前,android获取当前步数

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 classMainActivity extends Activity {privateTextView mStepTV;privateSensorManager mSensorManager;privateMySensorEventListener mListener;private int mStepDetector = 0; //自应用运行以来STEP_DETECTOR检测到的步数

private int mStepCounter = 0; //自系统开机以来STEP_COUNTER检测到的步数

@Overrideprotected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mStepTV=(TextView)findViewById(R.id.tv_step);

mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);

mListener= newMySensorEventListener();

}

@Overrideprotected voidonResume() {

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);

}

@Overrideprotected voidonPause() {

super.onPause();

mSensorManager.unregisterListener(mListener);

}classMySensorEventListener implements SensorEventListener {

@Overridepublic 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);

}

@Overridepublic void onAccuracyChanged(Sensor sensor, intaccuracy) {

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值