제손으로 허접스럽게 짠 코딩이지만 봐주셔서감사합니다
제가 gps를 일정시간마다 서버 mysql에 저장하려고 하는데요? (한번돌아갈때는 gps정도 잘구해서 잘저장합니다)
다음과같이 while에 try구문 그리고 타임스레드 섞어서 일정시간 반복문이 돌아가게만들었습니다.
그랬더니 같은값만 계속 똑같이 저장하는거 아니겠습니까? 변수초기화를해주면 0으로 계속저장되어버리고
문제점이 무엇을까요?
/**
* Program : GPS 위치 정보 구하기
* Author : gxgsung
* Date : 2010.12.31
* HomeUrl : gxgsung.blog.me
*/
package com.android.location1;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import android.app.*;
import android.os.Bundle;
import android.content.*;
import android.location.*;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.TimerTask;
public class LocationDemo extends Activity implements LocationListener {
private LocationManager locManager;
Geocoder geoCoder;
private Location myLocation = null;
double latPoint = 0;
double lngPoint = 0;
float speed = 0;
String SERVER_ADDRESS = "http://143.243.245.102/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// LocationListener의 핸들을 얻음
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// GPS로 부터 위치정보를 업데이트 요청, 1초마다 5km 이동시
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, this);
// 기지국으로 부터 위치정보를 업데이트 요청
//locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 5, this);
// 주소를 확인하기 위한 Geocoder KOREA 와 KOREAN 둘다 가능
geoCoder = new Geocoder(this, Locale.KOREAN);
final ToggleButton gpsButton = (ToggleButton) findViewById(R.id.gpsButton);
gpsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if( gpsButton.isChecked()) {
//while(true){
try {
GetLocations();
URL url = new URL(SERVER_ADDRESS + "/sensormonitor/insert.php?lat="
+ latPoint + "&lng="
+ lngPoint + "&speed="+ speed+ "&juso="+"juso");
url.openStream(); // 서버의 login.php파일을 실행함
Toast.makeText(getApplicationContext(), "서버전송성공.", Toast.LENGTH_SHORT).show();
Thread.sleep(3000);
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "서버전송실패.", Toast.LENGTH_SHORT).show();
}
// }
}
}
});
}
public void GetLocations() {
// 텍스트뷰를 찾음
latPoint = myLocation.getLatitude();// 요기가 핵심적으로 위도경도 받는부분이야
lngPoint = myLocation.getLongitude();
speed = (float)(myLocation.getSpeed() * 3.6);
TextView latText = (TextView) findViewById(R.id.tvLatitude);
TextView lngText = (TextView) findViewById(R.id.tvLongitude);
TextView speedText = (TextView) findViewById(R.id.tvSpeed);
TextView jusoText = (TextView) findViewById(R.id.tvAddress);
TextView test = (TextView) findViewById(R.id.test);
StringBuffer juso = new StringBuffer();
if (myLocation != null) {
try {
// 위도,경도를 이용하여 현재 위치의 주소를 가져온다.
List<Address> addresses;
addresses = geoCoder.getFromLocation(latPoint, lngPoint, 1);
for(Address addr: addresses){
int index = addr.getMaxAddressLineIndex();
for(int i=0;i<=index;i++){
juso.append(addr.getAddressLine(i));
juso.append(" ");
}
juso.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
latText.setText(String.valueOf(latPoint));
lngText.setText(String.valueOf(lngPoint));
speedText.setText(String.valueOf(speed));
jusoText.setText(String.valueOf(juso));
String t = "";
t = String.valueOf(latPoint) + "," + String.valueOf(lngPoint) + "," + String.valueOf(speed) + "\n" + String.valueOf(juso) + "\n";
Toast.makeText(this,t, Toast.LENGTH_SHORT).show();
test.append(t);
}
public void onLocationChanged(Location location) {
Log.d("location", "location changed");
myLocation = location;
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
public void onStatusChanged(String s, int i, Bundle bundle) {
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"프로그램 소개");
menu.add(0,2,0,"종료");
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();
System.exit(0);
case KeyEvent.KEYCODE_HOME:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
}
return super.onKeyDown(keyCode, event);
}
}