【问题标题】:How to monitor geofences in background on Oreo?如何在奥利奥后台监控地理围栏?
【发布时间】:2018-11-30 13:50:47
【问题描述】:

我遵循本教程:https://developer.android.com/training/location/geofencing 并且在 Android

当应用在后台时,如何获取地理围栏转换触发器?

我也试过用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 geolocation gps geofencing android-geofence


【解决方案1】:

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

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

处理地理围栏转换 当定位服务检测到用户已进入或退出地理围栏时,它会发送您在添加地理围栏的请求中包含的 PendingIntent 中包含的 Intent。此 Intent 由诸如 GeofenceTransitionsIntentService 之类的服务接收,该服务从 Intent 中获取地理围栏事件,确定地理围栏转换的类型,并确定触发了哪些已定义的地理围栏。然后它发送一个通知作为输出。

注意:在 Android 8.0(API 级别 26)及更高版本中,如果应用在监控地理围栏时在后台运行,则设备每隔几分钟就会响应一次地理围栏事件。要了解如何使您的应用适应这些响应限制,请参阅后台位置限制。

注册地理围栏服务后,它仍然存在,您无事可做,只需检查您的 IntentService 是否有特定的 PendingIntent,排除设备重启时需要重新注册您的地理围栏服务。

同时检查:https://developer.android.com/about/versions/oreo/background-location-limits

【讨论】:

【解决方案2】:

我使用 dexter 库进行权限地理围栏,这项工作适用于 android 8 9 10 及更高版本,您必须添加后台权限

 Dexter.withActivity(this@Activity_Map)
            .withPermissions(
                    Manifest.permission.ACCESS_COARSE_LOCATION
                    ,Manifest.permission.ACCESS_FINE_LOCATION
                    ,Manifest.permission.ACCESS_BACKGROUND_LOCATION
            )
            .withListener(object: MultiplePermissionsListener {
                override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                    report?.let {
                        if(report.areAllPermissionsGranted()){
                  //put your code here
                            }
                }

【讨论】:

  • 即使添加了权限,应用也可以在没有前台定位服务的情况下读取位置,就像打开谷歌地图应用一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 2020-02-14
  • 2018-06-19
  • 1970-01-01
相关资源
最近更新 更多