简体   繁体   English

应用程序后台的Android位置监听器

[英]Android location listener in Application background

I want to Implement a location listener in my app which will run in the background, even when the app is closed.我想在我的应用程序中实现一个位置侦听器,它会在后台运行,即使应用程序关闭也是如此。 I found a lot of result for implementing the location listener but I'm still confused.我发现了很多实现位置侦听器的结果,但我仍然感到困惑。

My use-case is to get the location in all possible ways, whether it'd be GPS, Network provider or any other way.我的用例是以所有可能的方式获取位置,无论是 GPS、网络提供商还是任何其他方式。 The app needs to update location periodically in the background该应用程序需要在后台定期更新位置

I found an example similar to below to get the precise location:我找到了一个类似于下面的示例来获取精确位置:

    LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

Few more examples at the link below:以下链接中的更多示例:

https://zgrepcode.com/examples/android/android/location/locationmanager-implementations https://zgrepcode.com/examples/android/android/location/locationmanager-implementations

this doesn't work very accurately, the listener didn't work in the background service.这不能很准确地工作,侦听器没有在后台服务中工作。

So is this the best way to get the location in the background?那么这是在后台获取位置的最佳方式吗? Most of the posts I found seem old, do we have a better way to implement background location listener in newer API's?我发现的大多数帖子看起来都很旧,我们是否有更好的方法在较新的 API 中实现后台位置侦听器?

I searched StackOverflow for the same but didn't find any relevant post, if there's a post which answers the same question, please share the link.我在 StackOverflow 上搜索了相同但没有找到任何相关帖子,如果有回答相同问题的帖子,请分享链接。 Or let me know whats the best way to implement it.或者让我知道实现它的最佳方法是什么。

Thanks in advance :-)提前致谢 :-)

Apps can receive location updates only a few times each hour . 应用每小时只能接收几次位置更新。 Google is very protective regarding this information. Google对于此信息非常保护。 Also active GPS, drains a lot of battery, so AGPS, is maybe better option if this is done manually. 主动GPS也会消耗大量电池,因此,如果手动完成操作,则AGPS可能是更好的选择。

Location updates are provided to background apps only a few times each hour. 每小时仅向几次后台应用提供位置更新。

An app is considered to be in the foreground if any of the following is true : 一个应用程序被认为是在前台 ,如果以下任一为

  • It has a visible activity, whether the activity is started or paused. 它具有可见的活动,无论该活动是开始还是暂停。
  • It has a foreground service. 它具有前台服务。
  • Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. 另一个前台应用程序通过绑定到其服务之一或通过使用其内容提供商之一而连接到该应用程序。 For example, if a foreground app binds to any of the following components within another app, that other app is considered to be in the foreground, input method editor (IME), Wallpaper service, Notification listener,Voice or text service. 例如,如果前台应用程序绑定到另一个应用程序中的以下任何组件,则该另一个应用程序将被视为位于前台,输入法编辑器(IME),墙纸服务,通知侦听器,语音或文本服务中。

If none of those conditions is true, the app is considered to be in the background. 如果所有这些条件都不成立,则认为该应用程序处于后台。

Consider using FusedLocationProviderClient , but that has a dependency on Google Play Services . 考虑使用FusedLocationProviderClient ,但是它依赖于Google Play服务 This is the best option. 这是最好的选择。 You can read more about it here 您可以在这里了解更多信息

I made an app by using Jobscheduler. 我使用Jobscheduler制作了一个应用程序。 It working. 工作正常。 But it has some conflict when the other app likenavigation sw is using location manager. 但是,当其他应用程序(例如)导航sw正在使用位置管理器时,就会发生冲突。 I am looking forthe solution now. 我现在正在寻找解决方案。

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

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