Android Q - 前台服务需要后台位置权限? [英] Android Q - Background Location Permission needed for Foreground service?

查看:105
本文介绍了Android Q - 前台服务需要后台位置权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文档中的第一点表示,如果应用在前台启动 Android 11 时启动前台位置服务,则需要后台位置权限.

The first point in this document says that Background Location permission is required if the foreground location service is started while the app is in foreground starting Android 11.

https://developer.android.com/guide/components/foreground-services#bg-access-restrictions

我的用例是用户点击按钮开始位置跟踪.点击该按钮会启动一个前台服务,该服务会发出通知.现在,我希望应用程序即使在后台运行后也能继续跟踪.在 Android 10 之前,完成此操作不需要后台位置权限.

My use case is that a user taps on a button to start location tracking. Tapping that button starts a foreground service which puts a notification. Now, I want the app to continue tracking even after backgrounding. Until Android 10, background location permission wasn't required to accomplish this.

我的问题是,要支持 Android 11,我是否还需要开始请求后台权限?另外,我需要为 Android 10 做同样的事情吗?没有在互联网上找到任何其他参考资料来验证这一点.请让我知道您的想法.

My question is that to support Android 11, do I need to start requesting background permission as well? Also, do I need to do the same for Android 10? Not finding any other reference on the internet to verify this. please let me know your thoughts.

推荐答案

根据定义后台工作

只要满足以下每个条件,应用就会被视为在后台运行:

An app is considered to be running in the background as long as each of the following conditions are satisfied:

  1. 用户当前无法看到该应用的所有活动.
  2. 应用没有运行任何在用户可见应用活动时启动的前台服务.

否则,该应用将被视为在前台运行.

Otherwise, the app is considered to be running in the foreground.

在您的情况下,您符合第二点,当用户可以看到活动时启动前台服务,因此从技术上讲,即使用户显示服务通知,您的应用仍被视为处于前台离开您的应用.

In your case, you fit into the second point where you started a foreground service when an activity is visible to the user so technically your app is still considered to be in the foreground while the service notification is being shown even if the user navigates away from your app.

如果您的目标是 Android 10 及更高版本,前台服务需要在其清单声明中包含 android:foregroundServiceType=location".请参阅此处.

The foreground service needs to have android:foregroundServiceType="location" in its manifest declaration if you are targeting Android 10 and above. See here.

如果您在创建的前台服务中获取位置更新,则您不一定需要 ACCESS_BACKGROUND_LOCATION 权限,因为您在技术上并未在后台访问位置.但是,在您的服务中,如果您使用任何可能需要后台位置权限的 API,例如地理围栏,那么您将需要将后台权限添加到清单中,并相应地请求和处理权限.

You don't necessarily need the ACCESS_BACKGROUND_LOCATION permission if you are fetching location updates in the foreground service that you have created since you are not technically accessing location while in the background. However, within your service, if you use any APIs that may require background location permission, such as Geofencing, then you will require the background permission to be added to the manifest and request and handle the permission accordingly.

这篇关于Android Q - 前台服务需要后台位置权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆