Android   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何监控Oreo背景中的地理围栏?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我按照本教程:https://developer.android.com/training/location/geofencing在Android<上工作正常8,但在奥利奥,由于新的操作系统背景限制,我遇到了问题. 当应用程序处于后台时,如何获得地理围栏转换触发器? 我也尝试使用BroadcastReceiver而不是Intentservice,但结果是一样的. 待定意图:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
    intent.action = "com.example.GEOFENCE_TRANSITION"
    PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_updatE_CURRENT)
}

注册地理围栏:

geofencingClient.addGeofences(request, geofencePendingIntent).run {
    addOnsuccessListener {
        Log.d(tag, "Geofence added")
    }
    addOnFailureListener {
        Log.e(tag, "Failed to create geofence")
    }
} 

广播接收器:

class GeofenceBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        Log.d(tag, "onReceive")
    }
}

清单中的接收者:

<receiver android:name=".GeofenceBroadcastReceiver">
    <intent-filter>
        <action android:name="com.example.GEOFENCE_TRANSITION"/>
    </intent-filter>
</receiver>

谢谢

编辑:Intentservice版本

待定意图:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceIntentservice::class.java)
    PendingIntent.getservice(context, 0, intent, PendingIntent.FLAG_updatE_CURRENT)
}

意向服务:

class GeofenceIntentservice : Intentservice("GeofenceIntentservice") {
    override fun onHandleIntent(p0: Intent?) {
        Log.d(tag, "onHandleIntent")
    }
}

清单中的服务:

<service android:name=".GeofenceIntentservice"/>

解决方法:

后台达到地理围栏转换时,您应该在Android 8上每隔几分钟获得一次Intent.

见:https://developer.android.com/training/location/geofencing#java

一旦地理围栏服务被注册,它仍然存在,您没有其他任何事情要做,只检查您的Intentservice以获取特定的PendingIntent,排除设备重启时您需要重新注册您的地理围栏服务.

另请查看:https://developer.android.com/about/versions/oreo/background-location-limits

大佬总结

以上是大佬教程为你收集整理的android – 如何监控Oreo背景中的地理围栏?全部内容,希望文章能够帮你解决android – 如何监控Oreo背景中的地理围栏?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: