Code example for LocationManager: Finding current location

Methods: getBestProvidergetLastKnownLocationrequestSingleUpdate

0
 
    @Override 
    protected void onUpdateData(int reason) {
        Log.d(TAG, "sunrise onUpdateData: " + sLocationCriteria.toString());
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = lm.getBestProvider(sLocationCriteria, true);
        if (TextUtils.isEmpty(provider)) {
            Log.d(TAG, "No available location providers matching criteria." + lm.getAllProviders());
            return; 
        } 
 
        final android.location.Location lastLocation = lm.getLastKnownLocation(provider);
 
        if (lastLocation == null ||
                (SystemClock.elapsedRealtimeNanos() - lastLocation.getElapsedRealtimeNanos())
                        >= STALE_LOCATION_NANOS) {
            Log.d(TAG, "Stale or missing last-known location; requesting single coarse location "
                    + "update."); 
            disableOneTimeLocationListener(); 
            mOneTimeLocationListenerActive = true;
            lm.requestSingleUpdate(provider, mOneTimeLocationListener, null);
        } else { 
            publishUpdate(lastLocation);
        } 
 
    } 
Experience pair programming with AI  Get Codota for Java