안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요 안드로이드 완전 초짜 입니다.
다름이 아니라 오드로이드 보드에서 가속도 센서를 이용해 보려고 하는데요.
"프로페셔널 안드로이드 애플리케이션 개발"
이 책에서 10장에서 알려주는데로 작성을 했는데 오드로이드 보드에서 센서를 이용하지 못하는 것 같습니다.
0.0mph 에서 전혀 변화가 없더군요.
책에 써있는데로만 따라하면 안되는 건가요?
제가 뭘 잘못 알고 있는건가요?
고수님들의 답변 부탁드립니다.
import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class Speedometer extends Activity { /** Called when the activity is first created. */ SensorManager sensorManager; TextView myTextView; float appliedAcceleration = 0; float currentAcceleration = 0; float velocity = 0; Date lastUpdate; private void updateVelocity() { Date timeNow = new Date(System.currentTimeMillis()); long timeDelta = timeNow.getTime() - lastUpdate.getTime(); lastUpdate.setTime(timeNow.getTime()); float deltaVelocity = appliedAcceleration * (timeDelta / 1000); appliedAcceleration = currentAcceleration; velocity += deltaVelocity; double mph = (Math.round(velocity / 1.6 * 3.6)); myTextView.setText(String.valueOf(mph) + "mph"); } private final SensorListener sensorListener = new SensorListener() { double calibration = Double.NaN; public void onSensorChanged(int sensor, float[] values) { double x = values[SensorManager.DATA_X]; double y = values[SensorManager.DATA_Y]; double z = values[SensorManager.DATA_Z]; double a = -1*Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); if (calibration == Double.NaN) calibration = a; else { updateVelocity(); currentAcceleration = (float)a; } } public void onAccuracyChanged(int sensor, int accuracy) {} }; Handler handler = new Handler(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); myTextView = (TextView)findViewById(R.id.myTextView); lastUpdate = new Date(System.currentTimeMillis()); sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); sensorManager.registerListener(sensorListener, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_FASTEST); Timer updateTimer = new Timer("velocityUpdate"); updateTimer.scheduleAtFixedRate(new TimerTask() { public void run() { updateGUI(); } }, 0, 1000); } private void updateGUI(){ final double mph = (Math.round(100*velocity / 1.6*3.6)) / 100; handler.post(new Runnable(){ public void run() { myTextView.setText(String.valueOf(mph) + "mph"); } }); } }