简体   繁体   English

Android 位置在一段时间后停止更新位置

[英]Android location stops updating location after some time

I have an app, which main feature is to track user's workouts.我有一个应用程序,主要功能是跟踪用户的锻炼。 GPS location was working OK for past few years until this month, when strange issue appeared. GPS 位置在过去几年一直运行良好,直到本月出现了奇怪的问题。

Some people (not all), using different devices and android versions experience strange behaviour of gps location.有些人(不是全部)使用不同的设备和 android 版本遇到 gps 位置的奇怪行为。 Suddenly, after at least few minutes (sometimes few hours) onLocationChanged stops being fired.突然,在至少几分钟(有时几个小时)后 onLocationChanged 停止被解雇。 Sometimes it's working fine, then next day user has this issue again.有时它工作正常,然后第二天用户再次遇到此问题。

User has to start a new workout to get it back to work.用户必须开始新的锻炼才能使其恢复工作。 Also, in the app logs I found out that此外,在应用程序日志中,我发现

onConnectionSuspended

is called with cause = 1 (CAUSE_SERVICE_DISCONNECTED).调用原因 = 1 (CAUSE_SERVICE_DISCONNECTED)。

According to the documentation, lost connection should automatically reconnect, but it doesn't thus causing the described issue.根据文档,丢失的连接应该会自动重新连接,但它不会因此导致所描述的问题。

In order to fix that issue, I've created an internal timer (runnable), which checks when was the last time that location was updated.为了解决这个问题,我创建了一个内部计时器(可运行),它检查上次更新该位置的时间。 If it's more than, let's say, 2 minutes, it reconnects everything like this:如果超过 2 分钟,它会像这样重新连接所有内容:

mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
startLocationUpdates(); //calls requestLocationUpdates

It seems to be working, at least on one's of our test user devices.它似乎正在工作,至少在我们的测试用户设备上是这样。 Seems like a small hack to me, so let me ask you: have you experienced similar issue and do you have a better way to solve it?对我来说似乎是一个小技巧,所以让我问你:你是否遇到过类似的问题,你有没有更好的方法来解决它?

By the way, I'm using:顺便说一句,我正在使用:

com.google.android.gms:play-services-location:18.0.0

only thing I can think of is to restart gps receiver after not getting location for some time / start vibrating phone so user know and can restart his phone / try to register more providers not only gms.我唯一能想到的就是在一段时间未获得位置后重新启动 gps 接收器/开始振动手机,以便用户知道并可以重新启动他的手机/尝试注册更多的提供商,而不仅仅是 gms。 Try fused, classic GPS/network location listener.. if all of them send broadcast to some class and that class wait few seconds then prioritize the best one of those you got in time it would be great, I tried this.尝试融合的经典 GPS/网络位置监听器.. 如果它们都向某些 class 发送广播,并且 class 等待几秒钟,然后优先考虑你及时获得的最好的一个,那就太好了,我试过了。 it worked but i need to have accuracy like 3 meters.: :D so not possible for me它有效,但我需要有 3 米的精度。: :D 所以对我来说不可能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM