在Android 8或9上的背景中进行地理围栏不起作用

在Android 8或9上的背景中进行地理围栏不起作用,第1张

概述当他到达定义的区域时,我尝试向用户显示推送警报.所以我编写了我的应用程序:https://developer.android.comraining/location/geofencing如果我的应用程序在用户位置之后运行服务,它可以正常工作.如果我开始谷歌地图,它也会工作,也会跟踪我的位置.推送会出现.但是,如果我关闭

当他到达定义的区域时,我尝试向用户显示推送警报.

所以我编写了我的应用程序:https://developer.android.com/training/location/geofencing

如果我的应用程序在用户位置之后运行服务,它可以正常工作.

如果我开始谷歌地图,它也会工作,也会跟踪我的位置.推送会出现.

但是,如果我关闭我的应用程序,则不会显示推送,因此如果没有应用程序跟踪我的位置,则无法检测到地理围栏.

这是正常的吗?
如何让它始终有效?
如果您需要在您的位置之后使用前台服务,那么地理围栏的重点是什么?

 public voID createGeofenceAlerts(LatLng latLng, int radius) {    final Geofence enter = buildGeofence(ID_ENTER, latLng, radius, Geofence.GEOFENCE_Transition_ENTER);    final Geofence exit = buildGeofence(ID_EXIT, latLng, radius, Geofence.GEOFENCE_Transition_EXIT);    final Geofence DWell = buildGeofence(ID_DWELL, latLng, radius, Geofence.GEOFENCE_Transition_DWELL);    GeofencingRequest request = new GeofencingRequest.Builder()            .setinitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)            .addGeofence(enter)            .addGeofence(exit)            .addGeofence(DWell)            .build();    fencingClIEnt.addGeofences(request, getGeofencePendingIntent()).addOnSuccessListener(new OnSuccessListener<VoID>() {        @OverrIDe        public voID onSuccess(VoID aVoID) {            Timber.i("succes");            Toast.makeText(mContext, "Geofence added", Toast.LENGTH_LONG).show();        }    }).addOnFailureListener(new OnFailureListener() {        @OverrIDe        public voID onFailure(@NonNull Exception e) {            Timber.e(e,"failure");            Toast.makeText(mContext, "Geofence ERROR", Toast.LENGTH_LONG).show();        }    });}private PendingIntent getGeofencePendingIntent() {    Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class);    PendingIntent pending = PendingIntent.getService(            mContext,            0,            intent,            PendingIntent.FLAG_UPDATE_CURRENT);    return pending;}private Geofence buildGeofence(String ID, LatLng center, int radius, int TransitionType) {    Geofence.Builder builder = new Geofence.Builder()            // 1            .setRequestID(ID)            // 2            .setCircularRegion(                    center.getLatitude(),                    center.getLongitude(),                    radius)            // 3            .setTransitionTypes(TransitionType)            // 4            .setExpirationDuration(Geofence.NEVER_EXPIRE);    if (TransitionType == Geofence.GEOFENCE_Transition_DWELL) {        builder.setLoiteringDelay(LOITERING_DELAY);    }    return builder.build();}

解决方法:

我想我找到了一个在AndroID 9上测试过的解决方案.我使用了Google文档https://developer.android.com/training/location/geofencing但是我用广播接收器替换了这个服务.

我的GeofenceManager:

private val braodcastPendingIntent: PendingIntent    get() {        val intent = Intent(mContext, GeofenceTransitionsbroadcastReceiver::class.java)        val pending = PendingIntent.getbroadcast(                mContext.applicationContext,                0,                intent,                PendingIntent.FLAG_UPDATE_CURRENT)        return pending    } fun createGeofenceAlerts(latLng: LatLng, radiusMeter: Int, isbroadcast: Boolean) {    val enter = buildGeofence(ID_ENTER, latLng, radiusMeter, Geofence.GEOFENCE_Transition_ENTER)    val exit = buildGeofence(ID_EXIT, latLng, radiusMeter, Geofence.GEOFENCE_Transition_EXIT)    val DWell = buildGeofence(ID_DWELL, latLng, radiusMeter, Geofence.GEOFENCE_Transition_DWELL)    val request = GeofencingRequest.Builder()            .setinitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)            .addGeofence(enter)            .addGeofence(exit)            .addGeofence(DWell)            .build()    val pending = if (isbroadcast) {        braodcastPendingIntent    } else {        servicePendingIntent    }    fencingClIEnt.addGeofences(request, pending).addOnSuccessListener {        Timber.i("succes")        Toast.makeText(mContext, "Geofence added", Toast.LENGTH_LONG).show()    }.addOnFailureListener { e ->        Timber.e(e, "failure")        Toast.makeText(mContext, "Geofence ERROR", Toast.LENGTH_LONG).show()    }}private fun buildGeofence(ID: String, center: LatLng, radius: Int, TransitionType: Int): Geofence {    val builder = Geofence.Builder()            // 1            .setRequestID(ID)            // 2            .setCircularRegion(                    center.latitude,                    center.longitude,                    radius.tofloat())            // 3            .setTransitionTypes(TransitionType)            // 4            .setExpirationDuration(Geofence.NEVER_EXPIRE)    if (TransitionType == Geofence.GEOFENCE_Transition_DWELL) {        builder.setLoiteringDelay(LOITERING_DELAY)    }    return builder.build()}

我的broadcastReceiver,显然你需要在manfifest中声明它:

class GeofenceTransitionsbroadcastReceiver : broadcastReceiver() {overrIDe fun onReceive(context: Context, intent: Intent) {    Timber.i("received")    val geofencingEvent = GeofencingEvent.fromIntent(intent)    if (geofencingEvent.hasError()) {        Timber.e("Geofence error")        return    }    // Get the Transition type.    val geofenceTransition = geofencingEvent.geofenceTransition    // Test that the reported Transition was of interest.    if (geofenceTransition == Geofence.GEOFENCE_Transition_ENTER || geofenceTransition == Geofence.GEOFENCE_Transition_EXIT            || geofenceTransition == Geofence.GEOFENCE_Transition_DWELL) {        // Get the geofences that were triggered. A single event can trigger        // multiple geofences.        val triggeringGeofences = geofencingEvent.triggeringGeofences        // Get the Transition details as a String.        val geofenceTransitionDetails = GeofenceManager.getGeofenceTransitionDetails(                geofenceTransition,                triggeringGeofences, true        )        // Send notification and log the Transition details.        GeofenceManager.sendNotification(context, geofenceTransition, geofenceTransitionDetails)        Timber.i(geofenceTransitionDetails)    } else {        // Log the error.        Timber.e("UnkNown geo event : %d", geofenceTransition)    }}

重要的是要知道在AndroID 8和9上,地理围栏的延迟时间为2分钟.

总结

以上是内存溢出为你收集整理的在Android 8或9上的背景中进行地理围栏不起作用全部内容,希望文章能够帮你解决在Android 8或9上的背景中进行地理围栏不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1096266.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存