Update GPS Listener every 5 seconds

后端 未结 2 789
心在旅途
心在旅途 2021-01-23 09:31

I\'m supposed to update the listener every 5 seconds. But it doesn\'t. What could be wrong with my code?

Thanks a lot for any help!

package com.example.a         


        
相关标签:
2条回答
  • 2021-01-23 10:12

    By setting it to 5000 ms it doesn't necessarily mean that you will get the location every 5th second. The time is more or less a best case scenario.

    minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value. http://developer.android.com/reference/android/location/LocationManager.html

    Are you sure you have your activity running? If you want to recieve the location when the activity is sleeping you need a wake lock and a service.

    For example in a GPS app I created I put 0 ms to get it as soon as possible but when debugging and checking the logcat I sometimes get the location every minute or so, not every second.

    0 讨论(0)
  • 2021-01-23 10:12

    As stated by David Olsson, MINIMUM_TIME_BETWEEN_UPDATES only prevents the listener to be requested too often. If you want to regularly requestLocationUpdates, you should use Timer and TimerTask and have requestLocationUpdates run once every 5 seconds

    schedule(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
    
    0 讨论(0)
提交回复
热议问题