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 좌표 (잘 잡히니깐)로
잡아서 처리하려고 하는데 ㅠㅠ
도와주세용 ㅠㅠ