2011-07-05 3 views
0

내 앱에서 위치 서비스를 설정하려고합니다.몇 분 후에 GPS를 '포기'- 안드로이드

GPS 신호를 사용할 수없는 경우 (신호를 찾으려고 X 분 후에) 사용자에게 오류를 표시하고 싶습니다.

어떻게하면됩니까?

여기 내 코드는 지금까지의 -

// Acquire a reference to the system Location Manager 
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    // Define a listener that responds to location updates 
    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      // Called when a new location is found by the network location provider. 
      Log.d("loca", "accuracy - " + location.getAccuracy()); 
      Log.d("loca", "longtitude - " + location.getLongitude()); 
      Log.d("loca", "Latitude - " + location.getLatitude()); 
      Log.d("loca", "Provider - " + location.getProvider()); 
      if(isBetterLocation(location,bestLoc)) 
      { 
       Log.d("loca", "best loc - yes"); 
       bestLoc = location; 
      } 
      else 
       Log.d("loca", "best loc - no"); 


     } 

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

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 
     }; 



    // Register the listener with the Location Manager to receive location updates 
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,TWO_MINUTES , 300, locationListener); 
     Log.d("loca", "GPS"); 
    } 
    else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
    { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,TWO_MINUTES , 300, locationListener); 
      Log.d("loca", "Net"); 
    } 
    else 
     Log.d("loca", "Nothing"); 

어떻게 내가 신호를 찾고 시작했습니다 이후가되었습니다 얼마나 알 수 ?

감사합니다.

답변

2

ServiceCountDownTimer을 사용하십시오. 서비스를 시작할 때 위치 업데이트 요청을 시작하십시오. 바로 업데이트 요청을 시작할 때 정의 된 지속 시간 (아래 예제에서는 15 초)으로 CountDownTimer을 시작하십시오. 위치가 발견되면 모든 카운트 다운 틱을 확인하십시오. countdowntimer (onFinish)의 끝에서 위치가 발견되지 않으면 stopSelf을 사용하여 서비스를 중지하고 마지막으로 알려진 위치를 신속하게 확인합니다.

샘플 코드 조각는 시작하려면 :

//start requesting updates... 
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    startTimer(); 
} 
else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    startTimer(); 
} 
else { 
    //do nothing since nothing is enabled 
    stopSelf(); 
} 

//function to start the timer once you start requesting updates 
private void startTimer() { 
    if(countDownTimer != null) { 
     countDownTimer.cancel(); 
     countDownTimer = null; 
    } 
    countDownTimer = new CountDownTimer(15 * 1000L, 1000L) {//15 seconds max 
     @Override 
     public void onTick(long millisUntilFinished) { 
      if(didFindLocation) { 
       cancel(); 
      } 
     } 
     @Override 
     public void onFinish() { 
      if(!didFindLocation) { 
       stopSelf(); 
      } 
     } 
    }; 
    countDownTimer.start(); 
} 

//when your service stops 
@Override 
public void onDestroy() { 
    if(!didFindLocation) { 
     Location location = null; 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     } 
     if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && location == null) { 
      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     } 
     //do something with location if not null 
    } 
    locationManager.removeUpdates(this); 
    if(countDownTimer != null) { 
     countDownTimer.cancel(); 
     countDownTimer = null; 
    } 
    super.onDestroy(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    didFindLocation = true; 
    //do something with location 
} 
관련 문제