마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

GPS 신호를 잡으려고 TIMER로 좀 기다리고 싶은데. 2개쓸 수 없나요?

0 추천

 public void getLocation(){
timer=new Timer();
timer1= new Timer();


lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

 

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


// 일단 GPS 신호를 잡도록 조금 기다림 -> TIMER를 이용.
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);

if (!gps_enabled) { Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled", duration);
toast.show();
}




timer1.schedule(new LocationNet(), 3000); // 3초동안 못잡았다면 NETWORK 좌표를 잡자.


}

class LocationNet extends TimerTask{ //NETWORK 좌표 잡기위함

@Override
public void run() {
// TODO Auto-generated method stub


if (network_enabled){ // NETWORK 좌표를 잡았다!

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork); // 이부분이 계속 오류라 하네요 -,.-;; network_enabled 은 boolean 으로 전역변수로했는디.. 왜 이러는지모르겠어요..

}

if (!network_enabled) { Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled", duration);
toast.show();

}
timer.schedule(new GetLastLocation(), 3000); // GPS, NETWORK 둘다 못잡으면 Lastknown으로 output하자!

}



}

LocationListener locationListenerGps = new LocationListener() { // GPS 받았을때 처리
public void onLocationChanged(Location location) {
timer.cancel();

lat =location.getLatitude();
lng = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);

Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps enabled "+lat + "\n" + lng, duration);
toast.show();
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};


LocationListener locationListenerNetwork = new LocationListener() { // NETWORK 잡았을때 처리!
public void onLocationChanged(Location location) {
timer.cancel();

lat = location.getLatitude();
lng = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);

Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network enabled"+lat + "\n" + lng, duration);
toast.show();
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};


//extends TimerTask
class GetLastLocation extends TimerTask { // Lastknown 으로 값을 저장하자!

@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);

Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
{lat = gps_loc.getLatitude();
lng = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown "+lat + "\n" + lng, duration);
toast.show();
}
else
{lat = net_loc.getLatitude();
lng = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "+lat + "\n" + lng, duration);
toast.show();

}

}

if(gps_loc!=null){
{lat = gps_loc.getLatitude();
lng = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown "+lat + "\n" + lng, duration);
toast.show();
}

}
if(net_loc!=null){
{lat = net_loc.getLatitude();
lng = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "+lat + "\n" + lng, duration);
toast.show();

}
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "no last know avilable", duration);
toast.show();

}
}}

 

졸업하기전에 서버랑 소켓으로 DB에 있는거 넣다뺏다 하면서 (위치정보위주) 하고 싶은데...

여기서 계속 막히네요..

쓰레드 적인 문제같은데 잘 모르겠어요..

GPS 좌표 잡는데 조금 걸리는거 같아서 3초-4초 정도 기다려주고, 그래도 안잡히면 그냥 NETWORK, AP 좌표 (잘 잡히니깐)로

잡아서 처리하려고 하는데 ㅠㅠ

도와주세용 ㅠㅠ

ADINRIQ (120 포인트) 님이 2014년 5월 23일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...