Android的位置感知的通知位置、通知、Android

2023-09-04 10:42:42 作者:再可爱一点好了

我在寻找的位置感知通知的最佳技术。

I'm looking for best techniques for Location Aware notifications.

我的第一次尝试是使用LocationManager的addProximityAlert但它需要ACCESS_FINE_LOCATION(COARSE_LOCATION给了我一个SecurityException),并保持GPS始终打开和激活(至少在果冻豆),其消耗电池电量,速度非常快。这也似乎并不尊重检查一次,每4分钟的时候,屏幕上睡着了( addProximityAlert )。

My first try was to use LocationManager's addProximityAlert but it requires ACCESS_FINE_LOCATION (COARSE_LOCATION gives me a SecurityException) and keeps the GPS always ON and active (at least on Jelly Bean), which drains the battery very quickly. It also doesn't seems to respect the check only once every 4 minutes when the screen is asleep (addProximityAlert).

我想与PASSIVE_PROVIDER合作,收集其他应用程序的位置信息,并可能使用服务,要求COARSE_LOCATION每过一段时间(如每4分钟的时候,屏幕关闭,并且更加频繁的画面时,是,设备已插入)。

I'm thinking to work with the PASSIVE_PROVIDER to gather other applications location information, and maybe use a service to ask for COARSE_LOCATION every once in a while (like every 4 minutes when the screen is off, and more frequently when the screen is On and the device is plugged in).

要求是当用户去附近的一些地点,应用程序将触发一个通知。

The requirement is to when the user goes nearby some locations, the app will fire a notification.

任何人有实施或有任何建议在一个有效的和电池效率的方式来实现这一点?

Anyone have implemented or have suggestions to implement this in a effective and battery efficient way?

推荐答案

我建立这样一个应用程序(因此我的问题在办公时间Q&放大器; A关于如何衡量LocationManager的功耗),同时它的工作真的很好。 我最初打算使用内置的接近警戒,但奋斗在同一个问题,因为你认为它不够灵活。

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.

相反,我建立它使用LocationProvider轮询位置使用NETWORK_PROVIDER每五分钟,计算到期望的位置(多个)的距离,如果在靠近,触发一个通知的服务。 在有很多无线网络的城市,这给了我50米(报道)的精度。

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.

我也用PASSIVE_PROVIDER从其他应用的位置请求中受益。

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

如果我是附近一所希望的位置和所报告的准确度太粗,以决定我是否在指定邻近范围内或没有,我使用的是单一的GPS定位请求作为备份。我也用单一的GPS位置请求,如果NETWORK_PROVIDER超时。为了不具备GPS提供商耗尽电池,而我使用的地铁,它也有与指数退避超时。

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.

虽然我连接到WiFi网络,我认为我不动,并禁用位置提供在这段时间。

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

我的最后一次更改是使用15分钟(不精确重复),定时器为NETWORK_PROVIDER如果我能决定,我不能让任何一个保存位置的15分钟内(以便查询了5分钟INTERVALL没有意义)。这有助于降低功耗。

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.

我想使用甚至更长的时间,以节省更多的权力,但它不容易在这里找到一个启发,决定我是否能达到或一个我在这段时间内储存的位置没有,因为我可以不总是承担同样的移动速度(步行,乘坐火车或汽车旅行)。 但因为我不能够测量由我的应用程序的LocationManager的功耗,我不知道怎么评价的功耗在每一天的情况。

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.

我不介意,如果内置的API将提供所有的逻辑,但我得出的结论是没有,所以我建立了这个对我自己的。

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.

希望这有助于你。