Update location every 5sec android

2019-08-27 10:57发布

Hi I want get user location for every 5sec and user can change that duration like 10sec, 15sec upto 30sec i have spinner to choose this option

this is my request

 private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000) // 5 seconds
        .setFastestInterval(16) // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

this is initiate my start update

 private void startPeriodicUpdates(int intervel) {
    stopPeriodicUpdates();
    REQUEST.setInterval(intervel);
    mLocationClient.requestLocationUpdates(REQUEST, this);
    // mConnectionState.setText(R.string.location_requested);
}

I can pass user selected interval to startPeriodicUpdates() method like startPeriodicUpdates(5000), startPeriodicUpdates(15000). But they location update duration working fine 15sec and more than 15sec If i give 5sec or 10 sec the location update is running for every second

06-18 18:46:23.638: I/Location(2860): Location Changed Location[fused 65.966697,-18.533300 acc=4 et=+1h0m13s881ms alt=15.044444]
06-18 18:46:24.642: I/Location(2860): Location Changed Location[fused   65.966697,-18.533300 acc=4 et=+1h0m14s883ms alt=15.044444]
06-18 18:46:25.642: I/Location(2860): Location Changed Location[fused 65.966697,-18.533300 acc=4 et=+1h0m15s883ms alt=15.044444]

log show time interval for 23, 24, 25 please give your ideas.

1条回答
Bombasti
2楼-- · 2019-08-27 11:09

The setInterval method is not exact. This is mentioned in the documentation.

Also, use the same interval for setFastestInterval and setInterval to prevent your app from receiving location updates faster than the the interval you specified in setInterval.

Just add REQUEST.setFastestInterval(intervel); to your startPeriodicUpdates method.

查看更多
登录 后发表回答