Android感应器 [英] Android Sensor

查看:113
本文介绍了Android感应器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传感器应该如何检测到手机没有移动?由于电话静止不动,因此号码一直在运行.感谢您的提前帮助.



Hi, how is the sensor suppose to detect that there''s no movement coming from the phone? Because the phone is still, the numbers keep running. Thanks for the help in advance.



package com.example.testsensors;

import java.util.List;
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;
import android.widget.Toast;

public class TestSensors extends Activity implements SensorEventListener {
    private boolean theRegisteredSensor;
    private SensorManager theSensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        theRegisteredSensor = false;
        theSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    }
        protected void onStart() {
                super.onResume();
                List<sensor> sensors = theSensorManager
                .getSensorList(Sensor.TYPE_ACCELEROMETER);
                if (sensors.size() > 0) {
                        Sensor sensor = sensors.get(0);
                        theRegisteredSensor = theSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
                }
        }



        @Override
        protected void onPause() {
                if (theRegisteredSensor) {
                        theSensorManager.unregisterListener(this);
                        theRegisteredSensor = false;
                }
                super.onPause();
        }


        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            StringBuffer StringBuff = new StringBuffer();
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
                StringBuff.append("Accelerometer\n");
                StringBuff.append("X-axis:").append(event.values[0]).append("\n");
                StringBuff.append("Y-axis:").append(event.values[1]).append("\n");
                TextView tv = (TextView) findViewById(R.id.accero_counters);
                tv.setText(StringBuff.toString());
            Toast.makeText(getBaseContext()
            , "Movement is detected.",Toast.LENGTH_SHORT).show();
        }
   }
}
</sensor>

推荐答案

您必须考虑重力引起的加速度.这将始终在至少具有三个正交加速度计的设备中进行测量.

为了解决这个问题,您必须进行某种形式的校准,将设备以已知的方向放置并记下加速度计的读数.然后,在使用该设备时,您可以计算当前传感器读数与来自校准的读数之间的旋转和平移,以确定运动和方向.
You have to take into account acceleration due to gravity. This will always be measured in a device with at least three orthogonal accelerometers.

To account for this, you have to have some sort of calibration where you put the device in a known orientation and make note of the accelerometer readings. Then when using the device, you calculate the rotation and translation between the current sensor readings and the ones from the calibration to determine movement and orientation.


意思是?那么,如果没有检测到运动该怎么办?我该怎么用呢?
Meaning? So what I should do if there''s no movement detected? What should I use instead?


我该怎么做呢?那我该怎么办?
How do I go about doing this? What can I do then?


这篇关于Android感应器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆