yayaa / LocationManager

Simplify getting user's location for Android
807 stars 187 forks source link

requiredTimeInterval not being considered when keepTracking is enabled #82

Closed Fuhrmann closed 4 years ago

Fuhrmann commented 6 years ago

I'm using a Service to get the user location and keep track of it. This is my configuration:

override fun getLocationConfiguration(): LocationConfiguration {
    return LocationConfiguration.Builder()
            .useDefaultProviders(DefaultProviderConfiguration.Builder()
                    .acceptableAccuracy(10.0f)
                    .requiredTimeInterval(5 * 60 * 1000) // 5 minutes
                    .acceptableTimePeriod(5 * 60 * 1000) // 5 minutes
                    .setWaitPeriod(ProviderType.GPS, 20 * 1000) // 20 seconds
                    .setWaitPeriod(ProviderType.NETWORK, 20 * 1000) // 20 seconds
                    .gpsMessage("Turn GPS on?")
                    .build())
            .keepTracking(true)
            .build()
}

My onLocationChange method:

override fun onLocationChanged(location: Location) {
    MyAsyncTask(location.latitude, location.longitude).execute()
}

Then in MyAsyncTask I just send the data to my server.

I start the service in my MainActivity:

startService(Intent(this, MyService::class.java))

I specify that I want the location every 5 minutes and want to keep track of it. I setup the emulator GPS data with a KML file and start the emulator GPS playback:

image

As you can see in the image above the data have a delay of 2 seconds between every location update. Even with the 2 seconds delay on each update, I dont want to be notified every 2 seconds. I want to be notified in a interval of 5 minutes as I specified in the configuration builder. Is this possible? Am I missing something, maybe this is supposed to work this way?

Logs

5-01 01:06:45.546 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:47.565 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:49.609 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:51.607 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:53.652 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:55.713 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:57.704 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:06:59.734 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:01.739 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:03.807 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:05.832 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:07.847 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:09.895 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:11.911 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:13.968 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:15.995 10114-10150/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:17.981 10114-10146/? D/MyService$MyAsyncTask: Sending location to server
05-01 01:07:19.999 10114-10150/? D/MyService$MyAsyncTask: Sending location to server

Thank you for your help!

Fuhrmann commented 6 years ago

Ok, I've tested this right now using my own device and it seems the updates are being called more frequently, even I'm in the same place and not moving (my location its not changing), here are the logs:

05-01 01:26:07.562 17050-17050/package I/DefaultLocationProvider: GPS is already enabled, getting location...
05-01 01:26:07.563 17050-17050/package I/DefaultLocationProvider: LastKnowLocation is not usable.
    Ask for location update...
05-01 01:26:27.588 17050-17050/package I/DefaultLocationProvider: We waited enough for GPS, switching to Network provider...
05-01 01:26:27.591 17050-17050/package I/DefaultLocationProvider: Network is enabled, getting location...
05-01 01:26:27.596 17050-17050/package I/DefaultLocationProvider: LastKnowLocation is not usable.
05-01 01:26:27.597 17050-17050/package I/DefaultLocationProvider: Ask for location update..
05-01 01:21:03.662 16344-16552/package D/MyService$MyTask: Sending location to server
05-01 01:21:04.548 16344-16569/package D/MyService$MyTask: Sending location to server
05-01 01:21:05.500 16344-16583/package D/MyService$MyTask: Sending location to server
05-01 01:21:06.539 16344-16636/package D/MyService$MyTask: Sending location to server
05-01 01:21:07.511 16344-16552/package D/MyService$MyTask: Sending location to server
05-01 01:21:08.562 16344-16569/package D/MyService$MyTask: Sending location to server
05-01 01:21:09.546 16344-16583/package D/MyService$MyTask: Sending location to server
05-01 01:21:10.565 16344-16636/package D/MyService$MyTask: Sending location to server
05-01 01:21:11.533 16344-16552/package D/MyService$MyTask: Sending location to server
05-01 01:21:12.505 16344-16569/package D/MyService$MyTask: Sending location to server
05-01 01:21:13.524 16344-16583/package D/MyService$MyTask: Sending location to server
05-01 01:21:14.506 16344-16636/package D/MyService$MyTask: Sending location to server
05-01 01:21:15.486 16344-16552/package D/MyService$MyTask: Sending location to server
05-01 01:21:16.552 16344-16569/package D/MyService$MyTask: Sending location to server
05-01 01:21:17.532 16344-16583/package D/MyService$MyTask: Sending location to server
05-01 01:21:18.529 16344-16636/package D/MyService$MyTask: Sending location to server
05-01 01:21:19.662 16344-16552/package D/MyService$MyTask: Sending location to server
05-01 01:21:20.559 16344-16569/package D/MyService$MyTask: Sending location to server
05-01 01:21:24.555 16344-16583/package D/MyService$MyTask: Sending location to server
yayaa commented 6 years ago

Good catch! 👍 I assume it happens because, everytime I request a new update but don't cancel the previous one. Which initially it requests immediate update as timeInterval 0.

Fuhrmann commented 6 years ago

Nice! I'm using the AlarmManager instead for now! It works great.

yayaa commented 4 years ago

Sorry for late action, but this should solve the problem.

I'll try to do other bugfixes as well, and release 2.1.0 version as short term plan.

Fuhrmann commented 4 years ago

No problem, thanks for taking the time to fix this issue!