本文介绍了如何使用Android传感器来检测的速度有多快装置移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器人编程初学者,我都拿出上获得传感器数据取得了一些进展(来源$ C ​​$ C以下)。有人可以帮我检测的速度有多快我的Andr​​oid设备使用这些信息的运动?例如,如果我快速移动的设备,它会给我像10,如果我慢慢地移动它,我会像加速度计1.除,所有其他传感器也为我确定。

 包course.examples.Sensors.ShowValues​​;进口android.app.Activity;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.widget.TextView;公共类SensorRawAccelerometerActivity扩展活动器具
        SensorEventListener {    私有静态最终诠释UPDATE_THRESHOLD = 500;
    私人的SensorManager mSensorManager;
    私人传感器mAccelerometer;    私人TextView的mXValueView,mYValueView,mZValueView;
    私人长期mLastUpdate;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        mXValueView =(的TextView)findViewById(R.id.x_value_view);
        mYValueView =(的TextView)findViewById(R.id.y_value_view);
        mZValueView =(的TextView)findViewById(R.id.z_value_view);        //获取引用的SensorManager
        mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);        //获取参考加速度
        如果(空==(mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER)))
            完();    }    //注册监听器
    @覆盖
    保护无效onResume(){
        super.onResume();        mSensorManager.registerListener(这一点,mAccelerometer,
                SensorManager.SENSOR_DELAY_UI);        mLastUpdate = System.currentTimeMillis的();    }    //注销监听器
    @覆盖
    保护无效的onPause(){
        mSensorManager.unregisterListener(本);
        super.onPause();
    }    //处理新的阅读
    @覆盖
    公共无效onSensorChanged(SensorEvent事件){        如果(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER){            长actualTime = System.currentTimeMillis的();            如果(actualTime - mLastUpdate> UPDATE_THRESHOLD){                mLastUpdate = actualTime;                浮X = event.values​​ [0],Y = event.values​​ [1],Z = event.values​​ [2];                mXValueView.setText(将String.valueOf(X));
                mYValueView.setText(将String.valueOf(Y));
                mZValueView.setText(将String.valueOf(Z));            }
        }
    }    @覆盖
    公共无效onAccuracyChanged(传感器传感器,精度INT){
        // N / A
    }
}


解决方案

在速度(加速度)加速度计测量的变化,而不是速度本身。如果您正在寻找短距离你的运气了。长距离你可以从它给你一个标题和平均速度的GPS查询此数据。

I am a beginner at android programming and I have come up with some progress on getting the sensor data (Source code below). Can someone help me with detecting how fast my Android device is moving using these information? For example if I am moving the device fast, it will give me like 10, and if I am moving it slowly, I will get like 1. Aside from accelerometer, all other sensors are also OK for me.

package course.examples.Sensors.ShowValues;

import android.app.Activity;
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 SensorRawAccelerometerActivity extends Activity implements
        SensorEventListener {

    private static final int UPDATE_THRESHOLD = 500;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;

    private TextView mXValueView, mYValueView, mZValueView;
    private long mLastUpdate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mXValueView = (TextView) findViewById(R.id.x_value_view);
        mYValueView = (TextView) findViewById(R.id.y_value_view);
        mZValueView = (TextView) findViewById(R.id.z_value_view);

        // Get reference to SensorManager
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        // Get reference to Accelerometer
        if (null == (mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER)))
            finish();

    }

    // Register listener
    @Override
    protected void onResume() {
        super.onResume();

        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_UI);

        mLastUpdate = System.currentTimeMillis();

    }

    // Unregister listener
    @Override
    protected void onPause() {
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    // Process new reading
    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            long actualTime = System.currentTimeMillis();

            if (actualTime - mLastUpdate > UPDATE_THRESHOLD) {

                mLastUpdate = actualTime;

                float x = event.values[0], y = event.values[1], z = event.values[2];

                mXValueView.setText(String.valueOf(x));
                mYValueView.setText(String.valueOf(y));
                mZValueView.setText(String.valueOf(z));

            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // N/A
    }
}
解决方案

Accelerometers measure change in speed (acceleration), not speed itself. If you are looking for short distances you are out of luck. Long distances you can poll this data from the GPS which gives you a heading and average velocity.

这篇关于如何使用Android传感器来检测的速度有多快装置移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:50