日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 计步功能原理,Android开发——计步功能

發布時間:2023/12/20 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 计步功能原理,Android开发——计步功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、使用Android內置傳感器?TYPE_STEP_COUNTER?和?TYPE_STEP_DETECTOR

TYPE_STEP_COUNTER 記錄開機以來的總步數,適合用于開發計步器。

A sensor of this type returns the number of steps taken by the user since the last reboot while activated. The value is returned as a float (with the fractional part set to zero) and is reset to zero only on a system reboot. The timestamp of the event is set to the time when the last step for that event was taken. This sensor is implemented in hardware and is expected to be low power. If you want to continuously track the number of steps over a long period of time, do NOT unregister for this sensor, so that it keeps counting steps in the background even when the AP is in suspend mode and report the aggregate count when the AP is awake. Application needs to stay registered for this sensor because step counter does not count steps if it is not activated. This sensor is ideal for fitness tracking applications.

TYPE_STEP_DETECTOR 檢測到用戶走了一步就向SensorEventListener傳遞一個浮點值1.0

A sensor of this type triggers an event each time a step is taken by the user. The only allowed value to return is 1.0 and an event is generated for each step. Like with any other event, the timestamp indicates when the event (here the step) occurred, this corresponds to when the foot hit the ground, generating a high variation in acceleration. This sensor is only for detecting every individual step as soon as it is taken, for example to perform dead reckoning. If you only need aggregate number of steps taken over a period of time, register for?TYPE_STEP_COUNTER?instead.

布局只是一個TextView,代碼就略過了,下面是主程序:

package com.lee.sensordemo;

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

mStepTV = findViewById(R.id.step_tv);

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

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

}

}

}

計步效果:當用戶走了一定的步數后,兩個計步器的步數才會開始變化。區別在于每次行走時,TYPE_STEP_COUNTER 會緩存步數,當檢測到步數大于10步時,TYPE_STEP_COUNTER 會一次性加10步,后續的步數就實時變化,直到用戶停止行走。而TYPE_STEP_DETECTOR 不會緩存步數,只有用戶走了8~10步時,TYPE_STEP_DETECTOR 才會開始連續記錄步數,直到用戶停止行走。

總結

以上是生活随笔為你收集整理的android 计步功能原理,Android开发——计步功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。