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
Handle geofence transitions
When Location Services detects that the user has entered or exited a geofence, it sends out the Intent contained in the PendingIntent you included in the request to add geofences. This Intent is received by a service like GeofenceTransitionsIntentService, which obtains the geofencing event from the intent, determines the type of Geofence transition(s), and determines which of the defined geofences was triggered. It then sends a notification as the output.Note: On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes. To learn how to adapt your app to these response limits, see Background Location Limits.
一旦地理围栏服务被注册,它仍然存在,您没有其他任何事情要做,只检查您的IntentService以获取特定的PendingIntent,排除设备重启时您需要重新注册您的地理围栏服务.
另请查看:https://developer.android.com/about/versions/oreo/background-location-limits
标签:geofencing,android-geofence,android,gps,geolocation 来源: https://codeday.me/bug/20190910/1801030.html