博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android之使用传感器获取相应数据
阅读量:7220 次
发布时间:2019-06-29

本文共 4644 字,大约阅读时间需要 15 分钟。

Android的大部分手机中都有传感器,传感器类型有方向、加速度(重力)、光线、磁场、距离(临近性)、温度等。

  方向传感器:   Sensor.TYPE_ORIENTATION

  加速度(重力)传感器: Sensor.TYPE_ACCELEROMETER

  光线传感器:    Sensor.TYPE_LIGHT

  磁场传感器:   Sensor.TYPE_MAGNETIC_FIELD

  距离(临近性)传感器: Sensor.TYPE_PROXIMITY

  温度传感器:   Sensor.TYPE_TEMPERATURE

//获取某种类型的感应器

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

//注册监听,获取传感器变化值

sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

上面第三个参数为采样率:最快、游戏、普通、用户界面。当应用程序请求特定的采样率时,其实只是对传感器子系统的一个建议,不保证特定的采样率可用。

最快: SensorManager.SENSOR_DELAY_FASTEST

最低延迟,一般不是特别敏感的处理不推荐使用,该种模式可能造成手机电力大量消耗,由于传递的为原始数据,算法不处理好将会影响游戏逻辑和UI的性能。

游戏: SensorManager.SENSOR_DELAY_GAME

游戏延迟,一般绝大多数的实时性较高的游戏都使用该级别。

普通: SensorManager.SENSOR_DELAY_NORMAL

标准延迟,对于一般的益智类或EASY级别的游戏可以使用,但过低的采样率可能对一些赛车类游戏有跳帧现象。

用户界面: SensorManager.SENSOR_DELAY_UI

 

一般对于屏幕方向自动旋转使用,相对节省电能和逻辑处理,一般游戏开发中我们不使用。

 

使用传感器做应用的难点在于获取数据后如何处理,获得相应需要的效果,这里涉及很多数学物理的知识……

下面使用一个代码实例演示如何获取方向和磁场强度的数据:

package com.magnetic;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {    private TextView magneticView;        private TextView orientationView;        private TextView totalMageticView;    private SensorManager sensorManager;        private MySensorEventListener sensorEventListener;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                sensorEventListener = new MySensorEventListener();            magneticView = (TextView) this.findViewById(R.id.magneticView);            orientationView = (TextView) this.findViewById(R.id.orientationView);          totalMageticView=(TextView) this.findViewById(R.id.totalMageticView);        //获取感应器管理器            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);       }    @Override        protected void onResume()         {            //获取方向传感器            @SuppressWarnings("deprecation")        Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);            sensorManager.registerListener(sensorEventListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);                        //获取加速度传感器            Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);            sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);            super.onResume();        }     @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }        private final class MySensorEventListener implements SensorEventListener        {            //可以得到传感器实时测量出来的变化值            @Override            public void onSensorChanged(SensorEvent event)             {                   //得到方向的值                if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)                {                    float x = event.values[SensorManager.DATA_X];                          float y = event.values[SensorManager.DATA_Y];                          float z = event.values[SensorManager.DATA_Z];                      orientationView.setText("方向: " + x + ", " + y + ", " + z);                 }                //得到加速度的值                else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD)                {                    float x = event.values[SensorManager.DATA_X];                          float y = event.values[SensorManager.DATA_Y];                          float z = event.values[SensorManager.DATA_Z];                     magneticView.setText("合磁场强度X、Y、Z分量: " + x + ", " + y + ", " + z);                   //计算和磁场强度                float total=(float)(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z, 2)));                totalMageticView.setText("合磁场强度: " + total);             }          }            //重写变化            @Override            public void onAccuracyChanged(Sensor sensor, int accuracy)             {            }        }           //暂停传感器的捕获        @Override        protected void onPause()         {            sensorManager.unregisterListener(sensorEventListener);            super.onPause();        }            }

 

转载于:https://www.cnblogs.com/zhaoyanhaoBlog/p/9139563.html

你可能感兴趣的文章
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>
大型分布式网站架构技术总结
查看>>
矩阵求导与投影梯度相关问题
查看>>
SVN
查看>>
C语言编程写的一个http下载程序(王德仙)2012-04-08
查看>>
CCF201409-3 字符串匹配(100分)
查看>>
UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
查看>>
Project Euler Problem 9: Special Pythagorean triplet
查看>>
HDU5701 中位数计数【中位数】
查看>>
Python 深浅拷贝 (Shallow copy and Deep copy in Python)
查看>>
Axure
查看>>
屏幕截取工具
查看>>
C语言第七次作业---要死了----
查看>>
Jquery事件绑定冲突
查看>>
偶现bug如何处理?
查看>>