光照传感器
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); } } }