안드로이드 개발 질문/답변
(글 수 45,052)
프로페셔널 안드로이드 애플리케이션 개발 - 리토 마이어
요 책에 나와있는, 가속도센서를 이용한 속도계 구현 예제가, SensorListener에서 SensorEventListener로 바꿔줘야되잖아요?
그래서 어줍잖은 초보실력으로 이것저것 바꿔보았는데, 역시나 핸드폰에서는 속도는 커녕 아무반응이 없습니다.;;
전문가님들 도와주세요 ㅠㅠ 어느부분을 더 손봐야하는지 알려주시면 감사하겠습니다.
작성한 Java 파일 올립니다.
package kr.speespract;
import java.util.Date; import java.util.Timer; import java.util.TimerTask;
import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.widget.TextView;
public class SpeedPract extends Activity implements SensorEventListener {
Handler handler = new Handler();
SensorManager sensorManager; TextView myTextView;
float appliedAcceleration = 0; float currentAcceleration = 0; float velocity = 0; Date lastUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.myTextView);
lastUpdate = new Date(System.currentTimeMillis());
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
Timer updateTimer = new Timer("velocityUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
updateGUI();
}
}, 0, 1000);
}
private void updateGUI() {
// Convert from meters per second to miles per hour.
final double mph = (Math.round(100*velocity / 1.6 * 3.6))/100;
// Update the GUI
handler.post(new Runnable() {
public void run() {
myTextView.setText(String.valueOf(mph) + "mph");
}
});
}
private void updateVelocity() {
// Calculate how long this acceleration has been applied.
Date timeNow = new Date(System.currentTimeMillis());
long timeDelta = timeNow.getTime()-lastUpdate.getTime();
lastUpdate.setTime(timeNow.getTime());
// Calculate the change in velocity at the
// current acceleration since the last update.
float deltaVelocity = appliedAcceleration * (timeDelta/1000);
appliedAcceleration = currentAcceleration;
// Add the velocity change to the current velocity.
velocity += deltaVelocity;
}
private final SensorEventListener sensorEvent = new SensorEventListener() {
double calibration = Double.NaN;
public void onSensorChanged(SensorEvent event) {
double x = event.values[SensorManager.DATA_X];
double y = event.values[SensorManager.DATA_Y];
double z = event.values[SensorManager.DATA_Z];
double a = 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(Sensor sensor, int accuracy) {}
};
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
}
public SensorEventListener getSensorEvent() {
return sensorEvent;
}
}
*공지사항을 다 읽었습니다.




private void updateGUI() { runOnUiThread(new Runnable() { public void run() { final double mph = (Math.round(100*velocity / 1.6 * 3.6))/100; myTextView.setText(String.valueOf(mph) + "mph"); } }); }