博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 传感器
阅读量:6296 次
发布时间:2019-06-22

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

1. 光照传感器

activity_main.xml

MainActivity.java

package liqing.lightsensortest;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.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView lightLevel;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lightLevel = (TextView) findViewById(R.id.light_level);        //获取系统传感器的管理器        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 光照传感器        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);        // 光照传感器注册(register)监听,参数为(SensorEventListener实例,传感器对象,输出信息速率)        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);    }    private SensorEventListener listener = new SensorEventListener() {        @Override        public void onSensorChanged(SensorEvent event) {            float value = event.values[0];            lightLevel.setText("Current light level is " + value + " lx ");        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //注销监听            sensorManager.unregisterListener(listener);        }    }}

2. 加速器传感器(摇一摇)

activity_main.xml

MainActivity.java

public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView lightLevel;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lightLevel = (TextView) findViewById(R.id.light_level);        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 accelerometer(加速器)传感器        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);    }    private SensorEventListener listener = new SensorEventListener() {        @Override        public void onSensorChanged(SensorEvent event) {            //方向传感器SensorEvent的取值有X轴,Y轴,Z轴,去绝对值Math.abs()            float x = Math.abs(event.values[0]);            float y = Math.abs(event.values[1]);            float z = Math.abs(event.values[2]);            if (x > 15 || y > 15 || z > 15) {                lightLevel.setText("摇一摇 ");            }        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //注销监听            sensorManager.unregisterListener(listener);        }    }}

3.方向传感器(赛车游戏控制方向) 使用加速传感器和地磁传感器,通过getRotationMatrix()和getOrientation()得到XYZ旋转值

main_activity.xml

MainActivity.java

package liqing.lightsensortest;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.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.light_level);        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 accelerometer(加速器)传感器        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        //带到 magnetic_FIELD(地磁的_领域)传感器        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);        //注意(SENSOR_DELAY_GAME)传感器的速率改变的相当快        sensorManager.registerListener(listener, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);        sensorManager.registerListener(listener, magneticSensor, SensorManager.SENSOR_DELAY_GAME);    }    private SensorEventListener listener = new SensorEventListener() {        float[] accelerometerValues = new float[3];        float[] magneticValues = new float[3];        @Override        public void onSensorChanged(SensorEvent event) {            //注意SensorEvent是所以传感器的信息(包括类型和值)            switch (event.sensor.getType()) {                case Sensor.TYPE_ACCELEROMETER:                    //clone()克隆                    accelerometerValues = event.values.clone();                    break;                case Sensor.TYPE_MAGNETIC_FIELD:                    //clone()克隆                    magneticValues = event.values.clone();                    break;                default:                    break;            }            float[] R = new float[9];//矩阵的值            float[] values = new float[3];//X,Y,Z旋转的值            //getRotationMatrix(得到旋转矩阵)            SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);            //getOrientation(得到选中的值)            SensorManager.getOrientation(R, values);            textView.setText(                    "X=" + values[0] + "\n"                     + "Y=" + values[1] + "\n"                     + "Z=" + values[2] + "\n");        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //注销监听            sensorManager.unregisterListener(listener);        }    }}

转载于:https://www.cnblogs.com/520-1314/p/4802851.html

你可能感兴趣的文章
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
26.Azure备份服务器(下)
查看>>
mybatis学习
查看>>
LCD的接口类型详解
查看>>
Spring Boot Unregistering JMX-exposed beans on shutdown
查看>>
poi 导入导出的api说明(大全)
查看>>
Mono for Android 优势与劣势
查看>>
将图片转成base64字符串并在JSP页面显示的Java代码
查看>>
js 面试题
查看>>
sqoop数据迁移(基于Hadoop和关系数据库服务器之间传送数据)
查看>>
腾讯云下安装 nodejs + 实现 Nginx 反向代理
查看>>
Javascript 中的 Array 操作
查看>>
java中包容易出现的错误及权限问题
查看>>
AngularJS之初级Route【一】(六)
查看>>
服务器硬件问题整理的一点总结
查看>>
SAP S/4HANA Cloud: Revolutionizing the Next Generation of Cloud ERP
查看>>