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); } }}