Qt Positioning 在 Android 上
在 Android 服务中使用Qt Positioning
在服务中使用Qt Positioning 需要执行若干额外操作,具体操作取决于Android版本。以下各节将详细说明这些操作。
注意:自 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 权限(参见下图)。

© 2026 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.