Android的 - 最好的方式来实现在多个活动LocationListener的最好的、多个、来实现、方式

2023-09-05 05:28:06 作者:凶手在逃中

我一直停留在这个问题上相当长的一段时间。我的工作,在几个不同的活动使用的位置相当广泛的应用。我发现每例使用在每一个活动的独立LocationListener的。这是不可行的在我的处境。

I have been stuck on this problem for quite some time. I am working on an app that uses location quite extensively in several different Activities. Every example I have found uses a separate LocationListener in every Activity. This is not feasible in my situation.

我想知道什么是最有效的方式来跟踪用户的位置,在几个活动。现在我已经创建了一个实现LocationListener的,并使用广播更新在活动的基类的静态纬度和长字段的服务。这意味着,服务总是运行,这是不理想的。但是,如果我关闭该服务,然后重新启动它,只有当我需要的时候,它需要一定的时间来获得一个好位置。我需要在活动的onCreate的位置数据()。这是相同的,如果我尝试在活动的基类来实现它。如果我不断地在登记onResume /的onCreate监听器和注销它的onPause(),它需要太多的时间来开始接收更新。我也试图创造,我可以绑定到一个服务,所以它只有开始,当我需要的位置。但我有同样的问题,它绑定到服务,并开始获得更新的时间太长。

I am wondering what is the most efficient way to keep track of the user's location across several activities. Right now I have created a service that implements LocationListener and uses a broadcast to update static lat and long fields in an Activity base class. This means that the service is always running, which isn't ideal. But if I shut down the service and restart it only when I need it, it takes time to get a good location. I need the location data in the Activity's onCreate(). It's the same if I try to implement it in the activity base class. If I constantly register the listener in onResume/onCreate and unregister it in onPause(), it takes too much time to start receiving updates. I also tried to create a service that I could bind to, so it only starts when I need a location. But I have the same problem, it takes too long to bind to the service and start getting updates.

这是我使用的服务现在的工作,但是从我读过的一切,我不应该使用一个不断运行的服务,为这样的琐碎的事情。但是该应用程序的全部要点是提供基于用户的当前位置相关的数据。所以我刚才在后台运行,并定期提供更新的服务。这引起了我重新审视了设计,在这一点上的一个主要问题是,我最近发现,onProviderEnabled()没有得到,如果用户没有启动GPS应用程序打开,随后使其调用。在这种情况下的应用程序有没有办法识别,全球定位系统启用,因此它可以开始监听更新。

The service that I am using now works but from everything I've read, I shouldn't be using a constantly running service for trivial things like this. But the whole point of the app is to provide relevant data based on the user's current location. So I have a service that just runs in the background and provides updates periodically. The one major problem that has caused me to reexamine the design at this point is that I recently discovered that onProviderEnabled() doesn't get called if the user starts the app without GPS turned on and then subsequently enables it. In this scenario the app has no way of recognizing that GPS was enabled so it can start listening for updates.

我想我明白LocationManager和LocationListener的距离看例子,但我似乎不能将其应用到这种情况,我需要在多个活动位置数据。任何帮助或建议将不胜AP preciated。

I thought I understood LocationManager and LocationListener from looking at the examples but I can't seem to apply it to this situation where I need location data in multiple Activities. Any help or advice would be greatly appreciated.

推荐答案

使用绑定的服务实现,就像一个在的 SDK文档中本地服务样品。很显然,你已经熟悉了服务的优势,让您创建所有位置code只有一次。

The way that I would typically implement this requirement is using a bound Service implementation, like the one in the Local Service Sample in the SDK Documentation. Obviously you're familiar with the advantage of the Service allowing you to create all the location code only once.

访问通过绑定的服务允许服务启动和停止本身,因此不运行时,你的应用程序是不是在前台(它会尽快死于没有更多的活动绑定)。关键,国际海事组织,以使这项工作顺利是绑定在 ONSTART服务()和解除绑定在的onStop(),因为当你从一个活动到另一个移动这两个通话重叠(次活动前将第一个停止启动)。这从应用程序内走动时垂死保持服务,并且只让服务模时整个应用程序(或至少任何部分感兴趣位置)离开前景

Accessing the Service through Bindings allows the Service to start and stop itself so it isn't running when your application isn't in the foreground (it will die as soon as no more Activities are bound). The key, IMO, to making this work well is to BIND the service in onStart() and UNBIND in onStop(), because those two calls overlap as you move from one Activity to another (Second Activity Starts before the First one Stops). This keeps the Service from dying when moving around inside the app, and only lets the service die when the entire application (or at least any part interested in location) leaves the foreground.

通过绑定,您不必传递一个广播的位置数据,因为该活动可以直接调用的服务方法,以获取最新位置。然而,广播仍然是有利的,因为指示何时有新的更新可用的方法...但是这只会成为一个通知到聆听活动来调用的getLocation()方法上的服务。

With Bindings, you don't have to pass the Location data in a Broadcast, because the Activity can call methods directly on the Service to get the latest location. However, a Broadcast would still be advantageous as a method of indicating WHEN a new update is available...but this would just become a notifier to the listening Activity to call the getLocation() method on the Service.

我的$ 0.02希望帮助!

My $0.02. Hope that Helps!