仅在前台访问设备位置的权限

Permission to access device location in foreground only

我的应用仅在前台使用位置数据。因为Android10区分后台位置和前台位置,所以我想把manifest.xml里的权限设置的尽量简约,不打扰用户。

发行说明 (https://developer.android.com/about/versions/10/privacy/changes) 说明如下: 如果我将目标 SDK 设置为 Android 10 (29) 并且只在清单中请求 ACCESS_FINE_LOCATION (故意排除新的 ACCESS_BACKGROUND_LOCATION 权限),我应该只获得前台访问权限(这就是我想要)。

在成绩文件中:

compileSdkVersion 29
    defaultConfig {
        applicationId "com.genewarrior.sunlocator"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 70
        versionName "3.10"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }

在清单文件中:

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

但是,在位置权限中仍然可以选择“始终允许”,而不是预期的“仅在使用应用程序时允许”。

还时不时地弹出“应用程序在后台获取您的位置”的通知,这正是我想要避免的。

我觉得你做的是对的。您可以为您的应用请求仅位置权限以“仅在使用该应用时允许”。在请求代码上,您只需要请求 ACCESS_FINE_LOCATION 权限,在这种情况下:

ActivityCompat.requestPermissions(thisActivity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1234)

并且只在清单中放入 ACCESS_FINE_LOCATION。

您在应用位置设置中看到“始终允许”。正如@greywolf82 所建议的那样,您的某些依赖项中可能包含 ACCESS_BACKGROUND_LOCATION 权限。