Example usage for android.location Location getElapsedRealtimeNanos

List of usage examples for android.location Location getElapsedRealtimeNanos

Introduction

In this page you can find the example usage for android.location Location getElapsedRealtimeNanos.

Prototype

public long getElapsedRealtimeNanos() 

Source Link

Document

Return the time of this fix, in elapsed real-time since system boot.

Usage

From source file:org.y20k.trackbook.TrackerService.java

private void addWayPointToTrack() {

    // create new WayPoint
    WayPoint newWayPoint = null;/*from w w w.  j  a  v a  2 s  .  c  om*/

    // get number of previously tracked WayPoints
    int trackSize = mTrack.getWayPoints().size();

    if (trackSize == 0) {
        // add first location to track
        newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
    } else {
        // get last WayPoint and compare it to current location
        Location lastWayPoint = mTrack.getWayPointLocation(trackSize - 1);

        // default value for average speed
        float averageSpeed = 0f;

        // compute average speed if new location come from network provider
        if (trackSize > 1 && mCurrentBestLocation.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
            Location firstWayPoint = mTrack.getWayPointLocation(0);
            float distance = firstWayPoint.distanceTo(lastWayPoint);
            long timeDifference = lastWayPoint.getElapsedRealtimeNanos()
                    - firstWayPoint.getElapsedRealtimeNanos();
            averageSpeed = distance / ((float) timeDifference / ONE_NANOSECOND);
        }

        if (LocationHelper.isNewWayPoint(lastWayPoint, mCurrentBestLocation, averageSpeed)) {
            // if new, add current best location to track
            newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
        }

    }

    // send local broadcast if new WayPoint added
    if (newWayPoint != null) {
        sendTrackUpdate();
    }

}

From source file:net.imatruck.betterweather.BetterWeatherExtension.java

/**
 * Requests a location update if setting is Automatic, else it will give a dummy location
 *
 * @param lm       Location Manager from {@link net.imatruck.betterweather.BetterWeatherExtension#onUpdateData(int)}
 * @param provider Provider determined in {@link net.imatruck.betterweather.BetterWeatherExtension#onUpdateData(int)}
 *///from  w ww.ja v a2  s  .c  om
private void requestLocationUpdate(final LocationManager lm, final String provider) {
    if (provider != null && sUseCurrentLocation) {
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            handleMissingPermission();
            return;
        }
        final Location lastLocation = lm.getLastKnownLocation(provider);
        if (lastLocation == null || (SystemClock.elapsedRealtimeNanos()
                - lastLocation.getElapsedRealtimeNanos()) >= STALE_LOCATION_NANOS) {
            LOGW(TAG,
                    "Stale or missing last-known location; requesting single coarse location " + "update. "
                            + ((lastLocation != null)
                                    ? lastLocation.getLatitude() + ", " + lastLocation.getLongitude()
                                    : "Last location is null"));
            try {
                disableOneTimeLocationListener();
                mOneTimeLocationListenerActive = true;
                lm.requestSingleUpdate(provider, mOneTimeLocationListener, null);
                gpsFixHandler.postDelayed(new Runnable() {
                    public void run() {
                        disableOneTimeLocationListener();
                        LOGD(TAG, "We didn't get a GPS fix quick enough, we'll try again later");
                        scheduleRefresh(0);
                    }
                }, 30 * 1000);
                LOGD(TAG, "Requested single location update");
                if (lastLocation != null) {
                    new RefreshWeatherTask(lastLocation).execute();
                }
            } catch (Exception e) {
                LOGW(TAG, "RuntimeException on requestSingleUpdate. " + e.toString());
                scheduleRefresh(2);
            }
        } else {
            new RefreshWeatherTask(lastLocation).execute();
        }
    } else if (!sUseCurrentLocation) {
        LOGD(TAG, "Using set location");
        disableOneTimeLocationListener();
        Location dummyLocation = new Location(provider);
        new RefreshWeatherTask(dummyLocation).execute();
    } else {
        handleMissingPermission();
    }
}