从后台删除应用程序后,地理围栏不会触发(应用程序已终止状态)

时间:2019-03-26 14:35:06

标签: android android-8.0-oreo geofencing android-doze

我已经使用Geofence来跟踪用户位置而不是服务。当应用程序在后台或前台时,用户位置会更新。当应用程序从后台删除(杀死状态)时,位置不会更新。

我已经在设备上进行了测试-

a)LG K10-OS 8

b)MI-OS 7(启用了自动启动权限)

我已按照下面提到的链接使用Geofence更新位置 1。https://github.com/googlesamples/android-play-location/tree/master/Geofencing

2。https://codelabs.developers.google.com/codelabs/background-location-updates-android-o/index.html?index=..%2F..index#0

添加地理围栏

   @SuppressLint("MissingPermission")
   fun addOrUpdateGeofence() {
    try {
        val sessionMgmt = SessionMgmt(context)
        if (sessionMgmt.lat == "") return
        removeGeofence()
        val geofence = Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this geofence.
                .setRequestId(geofenceId)
                // Set the circular region of this geofence.
                .setCircularRegion(sessionMgmt.lat.toDouble(), sessionMgmt.lng.toDouble(), GEOFENCE_RADIUS_IN_METERS)
                // Set the expiration duration of the geofence. This geofence gets automatically
                // removed after this period of time.
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT or Geofence.GEOFENCE_TRANSITION_DWELL)
                // Create the geofence.
                .setLoiteringDelay(GEOFENCE_LOITERING_DELAY)
                .build()

        val builder = GeofencingRequest.Builder()
        // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
        // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
        // is already inside that geofence.
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
        // Add the geofences to be monitored by geofencing service.
        builder.addGeofence(geofence)
        // Return a GeofencingRequest.
        //    sendNotification("Adding Fence")
        mGeofencingClient.addGeofences(builder.build(), 
       getGeofencePendingIntent()).addOnCompleteListener(this)

        val location = Location("")
        location.latitude = sessionMgmt.lat.toDouble()
        location.longitude = sessionMgmt.lng.toDouble()
        val updateLocation = UpdateLocation()
        updateLocation.updateUserLocation(context, location)
        updateLocation.updateUserLocationSpanish(context, location)


    } catch (e: Exception) {
        e.printStackTrace()
    }

}

待定意向

 private fun getGeofencePendingIntent(): PendingIntent? {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent as PendingIntent
    }
    val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    mGeofencePendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    return mGeofencePendingIntent
}

实际结果

当应用处于终止状态时,Geofence不调用触发器。

实际结果

Appliaction处于终止状态时,是否是Geofence触发器。如果是,那么为什么不触发Geofence

0 个答案:

没有答案