我按照本教程: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
总结以上是内存溢出为你收集整理的android – 如何监控Oreo背景中的地理围栏?全部内容,希望文章能够帮你解决android – 如何监控Oreo背景中的地理围栏?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)