Android location aware notifications

混江龙づ霸主 提交于 2019-12-02 20:59:44

I built such an app (hence my question in the Office Hours Q&A about how to measure the power draw of the LocationManager) and meanwhile it's working really well. I initially intended to use the builtin proximity alert, but struggled over the same issue as you and thought that it's not flexible enough.

Instead, I built a service which uses the LocationProvider to poll the location every five minutes using the NETWORK_PROVIDER, calculates the distance to the desired location(s) and if within proximity, fires a notification. In cities with lots of Wifi networks, this gives me an (reported) accuracy of 50 meters.

I also use the PASSIVE_PROVIDER to benefit from other apps' location requests.

If I'm nearby a desired location and the reported accuracy is too coarse to decide whether I'm within the specified proximity range or not, I'm using single GPS location requests as backup. I also use single GPS location requests if the NETWORK_PROVIDER times out. To not have the GPS provider drain the battery while I'm using the subway, it also has a timeout with exponential backoff.

While I'm connected to a Wifi network, I assume that I'm not moving, and disable the location providers during that time.

The last change I made was to use a 15 minute ("inexact repeating") timer for the NETWORK_PROVIDER if I can determine that I will not be able make it to any of the saved locations within 15 minutes (so polling with a 5 minute intervall doesn't make sense). This helps saving power.

I was thinking about using an even longer interval to save even more power, but it's not easy to find a heuristic here that determines whether I would be able to reach one of my saved locations within that time or not, because I can not always assume the same traveling speed (walking, traveling by train or car). But as I'm not able to measure the power draw of the LocationManager caused by my app, I don't know how to evaluate the power comsumption in an every day scenario.

I wouldn't mind if the builtin API would provide all that logic, but I concluded that it does not, so I built this on my own.

Hope this helps you.

I am using the following library to do this. https://code.google.com/p/little-fluffy-location-library/

It is easy to use but it doesn't give much flexibility. You cannot change the update frequency on-the-fly for instance. But its source is available, you can convert it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!