开发者

'Giving up' on GPS after a few minutes -Android

开发者 https://www.devze.com 2023-03-17 17:14 出处:网络
I\'m trying to set up location services on my app. I\'d like to show the user an error in a case 开发者_如何学Cwhere there is no GPS signal available(After X minutes of trying to find a signal).

I'm trying to set up location services on my app.

I'd like to show the user an error in a case 开发者_如何学Cwhere there is no GPS signal available(After X minutes of trying to find a signal).

How can I do that?

Here's my code so far -

    // 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");

How can I know how long it has been since I've started looking for a signal?

Thanks!


Use a Service with a CountDownTimer. Begin requesting location updates when you start your service. right when you start requesting updates, start a CountDownTimer with a defined duration(in the example below, 15 seconds). check on every countdown tick if a location was found. at the end of the countdowntimer(onFinish), if no location was found stop the service using stopSelf and quickly check for a last known location.

Sample code snippets to get you started:

//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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消