后台位置更新 BroadcastReceiver 未触发

问题描述 投票:0回答:1

尽管 Android 文档强烈坚持不使用后台位置更新,但我的应用程序确实需要它们,因此我更新了所有内容以正确请求权限并遵守新规则。现在,一旦我获得

ACCESS_BACKGROUND_LOCATION
许可,我将执行以下操作以从我的初始片段中请求后台位置更新:

private fun configureBackgroundLocationTracking() {
    fusedLocationClient.requestLocationUpdates(createLocationRequest(), getPendingIntent())
}

private fun createLocationRequest(): LocationRequest {
    return LocationRequest.create().apply {
        interval = 20000
        fastestInterval = 10000
        priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
    }
}

private fun getPendingIntent(): PendingIntent {
    val intent = Intent(requireContext(), LocationUpdatesBroadcastReceiver::class.java)
    return PendingIntent.getBroadcast(
        requireContext(),
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )
}

在我的 AndroidManifest 中,我这样声明 BroadcastReceiver:

<receiver android:name=".LocationUpdatesBroadcastReceiver"
            android:exported="true"
            android:permission="android.permission.ACCESS_BACKGROUND_LOCATION">
    <intent-filter>
        <action android:name="com.herontrack.LOCATION_UPDATE" />
    </intent-filter>
</receiver>

这是我的 LocationUpdatesBroadcastReceiver 的代码:

class LocationUpdatesBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val result = LocationResult.extractResult(intent)
        if (result != null) {
            val location = result.lastLocation
            if (location != null) {
                Log.d(TAG, "Received location update: $location")
            }
        }
    }

    companion object {
        private const val TAG = "LUBroadcastReceiver"
    }
}

并且

onReceive()
中的日志指令将日志发送到远程记录器(Bugfender)。 当我的应用程序在前台时,一切似乎都正常,我可以看到日志。 但是当它在后台时,就没有更多的更新了。

我仔细检查了权限,我确定当我注册

BroadcastReceiver
时,
ACCESS_BACKGROUND_LOCATION
被授予。

我在运行 Android 10 的三星 S9 上运行。

我是不是忘记了什么?在 Fragment 中执行所有这些操作有问题吗?

android kotlin fusedlocationproviderapi
1个回答
0
投票

PendingIntent.FLAG_MUTABLE 或 PendingIntent.FLAG_UPDATE_CURRENT 我有同样的问题。对于后台位置更新最新的android需要显示通知

© www.soinside.com 2019 - 2024. All rights reserved.