开发者

Is requestForLocationupdates a blocking call in Android?

开发者 https://www.devze.com 2023-03-28 15:48 出处:网络
I want to update the location every 5 mins till the lifecycle of the application. I know how to update the location. What I want to know is whether the requestForLocation is a blocking call or not.

I want to update the location every 5 mins till the lifecycle of the application. I know how to update the location. What I want to know is whether the requestForLocation is a blocking call or not.

I can do this in Service class but Service is run on main thread only and also Service can be stopped by the system anytime. I do not want this to happen. The location update should work till the application开发者_如何学C is in memory. If the application itself is killed then obviously the Service also gets killed.

What should be the approach for implementing this?


What I want to know is whether the requestForLocation is a blocking call or not.

There is no method named requestForLocation() or requestForLocationUpdates() in Android. If you mean requestLocationUpdates(), this is not a blocking call.

I do not want this to happen.

Users and the OS have the right to terminate your service whenever they wish. In this case, it's a good thing, because your proposed plan wastes RAM for no benefit.

What should be the approach for implementing this?

Use AlarmManager and a service akin to my LocationPoller, so you can get your location data, do something with it, and shut down until the next period comes around.

Also, please allow your users to configure the polling period, including an option for "do not poll".


For some reason this requestLocationUpdates() call is really blocking, at least in the phone brands that I tested. The way to fix it is to use one of its overloads. Please see this answer in another post.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号