光照传感器
Posted zqxLonely
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了光照传感器相关的知识,希望对你有一定的参考价值。
package com.pingyijinren.test; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class IndexActivity extends AppCompatActivity { private TextView textView; private SensorManager sensorManager; private Sensor sensor; private SensorEventListener sensorEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); textView=(TextView)findViewById(R.id.textView); sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE); sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); sensorEventListener=new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { textView.setText("光照发生改变,当前光照为"+event.values[0]); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; sensorManager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onDestroy(){ super.onDestroy(); if(sensorManager!=null){ sensorManager.unregisterListener(sensorEventListener); } } }
以上是关于光照传感器的主要内容,如果未能解决你的问题,请参考以下文章
求大神看看Arduino代码 是关于GY-30光照强度传感器的代码