Qt Positioning 在安卓系统上

从 Android 服务使用Qt Positioning

根据 Android 版本的不同,从服务中使用Qt Positioning 需要执行几个额外的操作。下文将详细介绍这些操作。

注: 自 Android 8(API 级别 26)起,操作系统限制了应用程序在后台运行时检索用户当前位置的频率。应用程序通常每小时只能接收几次位置更新。更多信息,请参阅后台位置限制

使用前台服务

自 Android 8(API 级别 26)起,当应用程序进入后台时,Android 操作系统可以杀死后台服务。这通常会在后台运行约一分钟后发生。为了保持定位服务的运行,应将该服务作为前台服务来实现。这种服务会显示一个状态栏通知,在服务停止或从前台移除之前,该通知是无法解除的。这样,用户就可以随时了解重要的后台活动。

注意: 从 Android 9(API 级别 28)开始,前台服务需要一个特殊的FOREGROUND_SERVICE 权限。有关实施前台服务的更多详情,请参阅 Android 文档。

使用后台位置权限

自 Android 10(API 级别 29)起,服务必须请求ACCESS_BACKGROUND_LOCATION权限。应按如下方式将其添加到AndroidManifest.xml 文件中:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

注: 将权限添加到AndroidManifest.xml 后,仍需明确允许应用程序持续访问位置服务。为此,应导航至Settings ->Apps ,选择适当的应用程序,打开其权限,并指定Allow all the time 的位置权限(见下面的截图)。

详情请查看后台Android 文档中的访问位置

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.