GeofenceClient已成功添加地理围栏,但在输入三星围栏广播接收器的Samsung Galaxy S10e上未触发地理围栏

时间:2020-07-30 20:26:55

标签: android geolocation samsung-mobile geofencing android-geofence

在Samsung Galaxy s10e(Android 9和Android 10)上,尽管一切都在其他设备上运行(在Samsung a50,Samsung s8,Samsung s9等上测试),但BroadcastReceiver从未收到用户越过地理围栏的数据。 。尝试关闭电池优化,但即使在前台也没有任何反应。日志显示GeofencingClient成功添加了所有位置。也许有人遇到这样的问题?

private fun buildChildGeofencingRequest(geofences: List<Geofence>): GeofencingRequest? {
        return if(geofences.isNotEmpty()) {
            GeofencingRequest.Builder()
                            .addGeofences(geofences)
                            .build()
        } else null
    }


    private val geofencePendingIntent: PendingIntent by lazy {
        val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
        PendingIntent.getBroadcast(
                        context,
                        0,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
        )
    }

private fun buildGeoFence(geofence: GeofenceModel, type: GeoFenceType): Geofence? {

        val latitude = geofence.lat
        val longitude = geofence.lng
        val radius = geofence.radius

        val builder = Geofence.Builder()
                        .setRequestId(geofence.id)
                        .setCircularRegion(
                                        latitude,
                                        longitude,
                                        radius.toFloat()
                        ).setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                         .setExpirationDuration(Geofence.NEVER_EXPIRE) 

        return builder.build()
    }

MainActivity


    val geofencesRequest: GeofencingRequest? = {
            val geofences = mutableListOf<Geofence>()
            filteredGeofences.forEach { model ->
                //Build Geofence for each Geofence models.
                val geofence = buildGeoFence(model, GeoFenceType.CHILD)
                if (geofence != null) {
                    geofences.add(geofence)
                }
            }
            buildChildGeofencingRequest(filteredGeofencesWithParent)
        }()

        if(geofencesRequest != null) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                geofencingClient
                                .addGeofences(geofencesRequest, geofencePendingIntent)
                                .addOnSuccessListener {
                                    Log.d("Child Geofence", "Geofence Added Successfully ${filteredGeofences.map { "${it.id}, " }}")
                                }
                                .addOnFailureListener {
                                    context.clearPresenceData()
                                    Log.d("Child Geofence", "Geofence Addition Failed ${it.message}")
                                }
            }
        }

GeofenceBroadcastReceiver

class GeofenceBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val geofencingEvent = GeofencingEvent.fromIntent(intent)
        handleEvent(geofencingEvent, context)
    }

    private fun handleEvent(event: GeofencingEvent, context: Context) {

        //Child geofence Enter event
        if (event.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
Log.d("GEO","USER ENTERED ${event.triggeringGeofences}")
}

0 个答案:

没有答案