android - 应用程序未从 Android 10 上的前台服务发送位置

标签 android android-location android-10.0

我的应用程序面向 API 28。我有一个发送位置更新的前台服务。

当满足以下条件时,应用程序将停止发送位置:

  • 在 Android 10 上运行
  • 应用程序在后台运行
  • 位置权限被选择为“仅在使用应用程序时允许”

运行时

adb shell dumpsys package packageName

我看到了

android.permission.ACCESS_BACKGROUND_LOCATION: granted=false

从文档来看,“android.permission.ACCESS_BACKGROUND_LOCATION”权限似乎是向后兼容的。

如何解决这个问题?有没有办法禁用三态权限请求?

最佳答案

https://developer.android.com/about/versions/10/privacy/changes#app-access-device-location

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.

您正在使用前台服务来获取位置更新,但(我假设)您没有告诉操作系统,因此它认为您正在后台访问该位置。您需要做的就是声明您的前台服务进程位置。

<service android:name="com.example.MyForegroundService"
         android:foregroundServiceType="location"/>

现在,当您的服务运行时,操作系统会将您的应用视为位于前台,以便审核位置访问 - 并且您不需要 ACCESS_BACKGROUND_LOCATION

关于android - 应用程序未从 Android 10 上的前台服务发送位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61643442/

相关文章:

android - 如何在 Android 中通过 Twitter 分享链接?

java - 使用 pubnub 制作 Android 应用程序发送 GPS 信息,但应用程序崩溃并且没有显示错误

android - Location.getSpeed() 有效吗?

android - Xamarin Android 10 安装 APK - 找不到处理 Intent 的 Activity

Android Q - 在执行器上执行 AsyncTask 时出现 StackOverflowError

java - 如果有条件扩展一个类

android - 等待 AsyncTask 在 'PROCEEDING' 之前完成

android - 真的没有得到 setResult 和 onActivityResult

java - 永不关闭的位置后台服务

java - 错误:未找到Gradle DSL方法: 'imlementation()'