Fed*_*era 8 gps android geolocation geofencing android-geofence
我遵循了本教程:https : //developer.android.com/training/location/geofencing,并且在Android <8上可以正常工作,但是在Oreo中,由于新的操作系统背景限制,我遇到了问题。
How can I get geofence transition triggers when app is in background?
I also tried to use a BroadcastReceiver instead of IntentService, but the result is the same.
Pending Intent:
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)
}
Run Code Online (Sandbox Code Playgroud)
Register geofence:
geofencingClient.addGeofences(request, geofencePendingIntent).run {
addOnSuccessListener {
Log.d(TAG, "Geofence added")
}
addOnFailureListener {
Log.e(TAG, "Failed to create geofence")
}
}
Run Code Online (Sandbox Code Playgroud)
Broadcast Receiver:
class GeofenceBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
Log.d(TAG, "onReceive")
}
}
Run Code Online (Sandbox Code Playgroud)
Receiver in Manifest:
<receiver android:name=".GeofenceBroadcastReceiver">
<intent-filter>
<action android:name="com.example.GEOFENCE_TRANSITION"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
Thanks
Pending Intent:
private val geofencePendingIntent: PendingIntent by lazy {
val intent = Intent(context, GeofenceIntentService::class.java)
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
Run Code Online (Sandbox Code Playgroud)
Intent Service:
class GeofenceIntentService : IntentService("GeofenceIntentService") {
override fun onHandleIntent(p0: Intent?) {
Log.d(TAG, "onHandleIntent")
}
}
Run Code Online (Sandbox Code Playgroud)
Service in Manifest:
<service android:name=".GeofenceIntentService"/>
Run Code Online (Sandbox Code Playgroud)
在 Android 8 上,当您在后台实现地理围栏转换时,您应该每隔几分钟就会收到一个 Intent。
\n\n请参阅: https: //developer.android.com/training/location/geofencing#java
\n\n\n\n\n处理地理围栏转换\n当定位服务检测到用户已进入或退出地理围栏时,它会发出添加地理围栏请求中包含的 PendingIntent 中包含的 Intent。此意图由像 GeofenceTransitionsIntentService 这样的服务接收,该服务从意图中获取地理围栏事件,确定地理围栏转换的类型,并确定触发了哪个定义的地理围栏。然后它发送通知作为输出。
\n\n注意:在 Android 8.0(API 级别 26)及更高版本上,如果应用程序在监控地理围栏时在后台运行,则设备每隔几分钟响应一次地理围栏事件。要了解如何使您的应用适应这些响应限制,请参阅后台位置限制。
\n
注册地理围栏服务后,它\xc2\xb4s仍然存在,您无需执行其他操作,只需检查 IntentService 中的特定 PendingIntent,排除设备重新启动时您需要重新注册地理围栏服务的情况。
\n\n另请检查:https ://developer.android.com/about/versions/oreo/background-location-limits
\n| 归档时间: |
|
| 查看次数: |
3548 次 |
| 最近记录: |