gps 위도, 경도를 받아서 데이터베이스에 저장해주는 소스인데요.
빨간색으로 된 줄 보시면 5초마다 갱신을 해주었는데 log가 찍힌 시간을 보니 45초마다 갱신이 되던데요..
왜그런가요??
public class GPSservice extends Activity {
static JSONParser jParser = new JSONParser();
private static String ulati,ulongi;
private static String url_register = "http:/"IP"/gps.php";
//private static final String TAG_LOGIN_SUCCESS = "success";
private List<NameValuePair> params;
//================================================
//=============================================
TextView statusText, result;
//위치정보를 공급하는 근원
String locationProvider;
//위치 정보 매니져 객체
LocationManager locationManager;
// int count; //위치 정보 갱신 횟수를 세기 위한 변수
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
//필요한 객체의 참조값 얻어오기
statusText = (TextView)findViewById(R.id.status);
result = (TextView)findViewById(R.id.result);
//위치 정보 매니져 객체 얻어오기
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//위치정보 공급자 얻어오기
locationProvider = locationManager.getBestProvider(new Criteria(), true);
// Toast.makeText(this, "위치정보 공급자:"+locationProvider, 0).show();
//가장 최근의 Location 객체 얻어오기
Location location = locationManager.getLastKnownLocation(locationProvider);
}
//==================================================
//====================================================================
@Override
protected void onResume() {
super.onResume();
//(위치 정보 공급자, 갱신주기, 갱신거리, 리스너)
locationManager.requestLocationUpdates(locationProvider, 5000, 0, listener);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(listener);
}
//locationListener객체 만들기
LocationListener listener = new LocationListener() {
//상태가 바뀌었을 때
public void onStatusChanged(String provider, int status, Bundle extras) {
String msg = "";
switch(status){
case LocationProvider.OUT_OF_SERVICE :
msg = "서비스 지역이 아닙니다.";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE :
msg = "일시적으로 위치 정보를 사용할 수 없습니다.";
break;
case LocationProvider.AVAILABLE :
msg = "서비스 가능 지역입니다.";
break;
}
statusText.setText(msg);
}
//공급자가 공급 가능하게 되었을 때
public void onProviderEnabled(String provider) {
statusText.setText("현재 상태: 위치정보 이용가능");
}
//공급자가 공급 못하게 되었을 때
public void onProviderDisabled(String provider) {
statusText.setText("현재 상태: 위치정보 이용불가");
}
//위치 정보가 바뀌엇을때 호출되는 메소드
public void onLocationChanged(Location location) {
// count++;
double mylati= location.getLatitude();
String lati = Double.toString(mylati);
double mylong= location.getLongitude();
String longi = Double.toString(mylong);
// StringBuffer buffer = new StringBuffer();
// buffer.append("수신횟수:"+count+"\r\n");
// buffer.append("위도:"+location.getLatitude()+"\r\n");
// buffer.append("위도:"+location.getLongitude()+"\r\n");
//위치정보 출력하기
// result.setText(buffer.toString());
AlertDialog.Builder builder = new AlertDialog.Builder(
GPSservice.this);
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "moon"));
Log.e("a:","moon");
Log.e("b:",lati);
Log.e("c:",longi);
params.add(new BasicNameValuePair("latitude", lati));
params.add(new BasicNameValuePair("longitude", longi));
////////////// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_register, "GET", params);
}
};
}