Android 中光线传感器的使用详解

简介: Android 中光线传感器的使用详解

一、首先是布局页面activity_light_sensor.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LightSensorActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="光线传感器"
        android:textColor="@color/black"
        android:textSize="20sp" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

二、在对应的Activity中获取光线传感器的值LightSensorActivity,具体注释已经在代码中给出

public class LightSensorActivity extends AppCompatActivity implements SensorEventListener {
    private EditText editText;
    //传感器管理器对象
    private SensorManager sensorManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light_sensor);
        editText = findViewById(R.id.editText);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        //第一个参数:SensorEventListener对象用this来指定就可以了
        // 第二个参数:传感器对象 光线传感器类型的常量:TYPE_LIGHT
        // 第三个参数:传感器数据的频率 这里采用适合游戏的频率
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME);
    }
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }
    //当传感器的值,发生变化时,回调的方法
    @Override
    public void onSensorChanged(SensorEvent event) {
        //获取传感器的值
        float[] values= event.values;
        //获取传感器类型
        int sensorType = event.sensor.getType();
        StringBuilder stringBuilder = null;
        if (sensorType==Sensor.TYPE_LIGHT){
            stringBuilder = new StringBuilder();
            stringBuilder.append("光的强度值:");
            //添加获取的传感器的值
            stringBuilder.append(values[0]);
            editText.setText(stringBuilder.toString());
        }
    }
    //当传感器的精度,发生变化时,回调的方法
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
}

效果如图所示:

以上是光线传感器的简单使用,有不当的地方,可以在评论区指正~


目录
相关文章
|
4月前
|
传感器 物联网 Android开发
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
69 1
|
8月前
|
传感器 Android开发
Android 加速度传感器的使用详解
Android 加速度传感器的使用详解
206 0
|
8月前
|
传感器 Android开发
Android 中磁场传感器的使用详解
Android 中磁场传感器的使用详解
125 0
|
传感器 物联网 Android开发
Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
239 0
Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
|
传感器 Java 定位技术