标签:android
public class MainActivity extends Activity {
private SensorManager sensorManager;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
textView.setText("光线强度是"+value+"勒克斯");
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
@Override
protected void onDestroy() {
if (sensorManager != null){
sensorManager.unregisterListener(sensorEventListener);
}
super.onDestroy();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android
原文地址:http://blog.csdn.net/lpjishu/article/details/47342849