package firebase.androidhive.info.sensortest;
import android.app.Activity;
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.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends Activity {
TextView t_light, t_proximity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t_light = new TextView(this);
t_proximity = new TextView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(t_light);
layout.addView(t_proximity);
setContentView(layout);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
SensorEventListener mListener = new SensorEventListener() {
//센서 값이 변하게 되면 이게 호출된다.
@Override
public void onSensorChanged(SensorEvent event) {
float[] v = event.values;
switch(event.sensor.getType()) {
case Sensor.TYPE_LIGHT:
t_light.setText("조도 = " + v[0]); //조도센서는 값이 하나밖에 없다.
break;
case Sensor.TYPE_PROXIMITY:
t_proximity.setText("근접 = " + v[0]);
break;
}
}
//센서 값이 오차가 있을 수 있다. 잘못된값?
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
//조도 센서
sensorManager.registerListener(mListener,
sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), //이렇게 받아와야함
SensorManager.SENSOR_DELAY_FASTEST); //갱신 주기가 가장 빠름
//SensorManager.SENSOR_DELAY_GAME; 이건 그냥 게임전용 센서
//근접 센서
sensorManager.registerListener(mListener,
sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY), //이렇게 받아와야함
SensorManager.SENSOR_DELAY_FASTEST); //갱신 주기가 가장 빠름
}
@Override
protected void onPause() {
super.onPause();
}
}
댓글