Android Q - 从后台启动前台定位服务

标签 android location foreground-service android-10.0

我不确定在这种情况下应该采取什么行为。

在文档中它说:

An app is considered to be accessing location in the background unless one of the following conditions is satisfied:

  • An activity belonging to the app is visible.
  • The app is running a foreground service that has declared a foreground service type of location.


在 google 提供的示例中,他们使用了一个启动前台服务的 Activity 在前台 :

https://github.com/android/location-samples/tree/master/LocationUpdatesForegroundService

在我的情况下,一旦蓝牙设备连接到我的设备(使用 list 注册的广播接收器),我需要开始跟踪位置 - 这可能在后台,或者在应用程序终止之后)。

所以,我在 list 中声明服务,就像他们说的那样:
 <service
       android:name=".MyForegroundService"
       android:foregroundServiceType="location"
       android:exported="false" />

问题是:

如果我的应用收到广播,在后台/终止 - 如果我启动前台服务,是否考虑在后台访问位置在前台访问位置 ?

我应该要求 ACCESS_BACKGROUND_LOCATION ?或 ACCESS_COARSE_LOCATION 在这种情况下应该足够了吗?

最佳答案

在 API 29 上,如果您有 android:foregroundServiceType"location" , 你不需要 ACCESS_BACKGROUND_LOCATION .在 Service 期间进行的任何位置访问正在运行被认为是“前台访问”。

但这假设您可以运行前台服务。您必须继续 user-initiated action某种意义上的。如果 Service已经在运行,那么它不会是一个问题; Service表示用户发起的 Action 的继续。如果您需要在 BT 设备连接时启动它,那么我不确定这是否符合条件。那可能行不通。 (如果您发现...请提出修改建议!)

需要 ACCESS_BACKGROUND_LOCATION 的应用程序示例权限将是监控您的 GPS 位置而没有 Activities 的权限。是可见的,没有 "location" -type 前台服务通知正在显示。在 Android Q 上,未经许可,这不会被允许持续很长时间(超过大约 30 秒)。

我相信操作系统也会分配 ACCESS_BACKGROUND_LOCATION到用户在位置权限对话框中选择“始终允许”的旧版应用程序。

关于Android Q - 从后台启动前台定位服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523469/

相关文章:

android - 我需要在未插入计算机时调试我的 Android 应用程序

amazon-web-services - 使用 Elasticsearch 地理功能查找最常见的位置?

将应用程序发送到后台后,Android BLE SCAN 在前台服务中的 30 秒内停止

android - 用户禁用通知时的前台服务

Android保存图像(经过一些处理)最佳方法

android - 为什么 getView 没有被调用?

android - Task.isSuccessful() 为 false,无法添加任何用户

android - 如果用户已经授予 ACCESS_FINE_LOCATION,是否需要 ACCESS_COARSE_LOCATION?

c# - 设置表单位置c#

Android - 将图像文件发送到服务器数据库