나의 현재 경위도 좌표를 실시간으로 갱신해서 화면에 뿌려주려고 하는데요.
아래 코드와 같이 무한으로 동작하는 쓰레드를 만들었는데, 처음 애플리케이션을 실행할 때 좌표값을 딱 한번만 받아오는 문제가 있습니다.
// 경위도 좌표를 가져오는 GpsInfo 클래스의 참조변수
private GpsInfo gps;
// 경위도 좌표값을 저장할 변수
private TextView latValue, longValue;
// 쓰레드 시작을 위한 핸들러
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
updateCoorThread();
}
};
// 무한 동작 쓰레드 생성
@Override
protected void onStart() {
super.onStart();
Thread coorThread = new Thread(new Runnable() {
@SuppressWarnings("InfiniteLoopStatement")
@Override
public void run() {
while (true) {
try {
handler.sendMessage(handler.obtainMessage());
Thread.sleep(1000);
} catch (Exception e) {
Log.e(TAG, "Thread exception has occurred.");
}
}
}
});
coorThread.start();
}
// 쓰레드의 실제 동작 구현
public void updateCoorThread() {
latValue = findViewById(R.id.latValue);
longValue = findViewById(R.id.longValue);
gps = new GpsInfo(MainActivity.this);
// GPS 정보 가져오기
if (gps.isGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String latitudeSix = String.format("%.6f", latitude);
String longtitudeSix = String.format("%.6f", longitude);
latValue.setText(latitudeSix);
longValue.setText(longtitudeSix);
} else {
// GPS를 사용할 수 없을 때
gps.showSettingsAlert();
}
}
어디가 잘못된 것인지 알고 싶습니다.