【问题标题】:Geofencing api not working in android with pending intent地理围栏 api 无法在具有待定意图的 android 中工作
【发布时间】:2022-07-12 11:24:57
【问题描述】:

我正在尝试实现简单的地理围栏应用,但它根本没有被触发。

这是我到目前为止所做的代码 -

class GeoFencingHelper(context: Context, private val task: Task, private val pendingIntent: PendingIntent){

private val geofencingClient = LocationServices.getGeofencingClient(context)

init {
    Timber.e(task.id.toString())
}

@SuppressLint("MissingPermission")
fun initiateGeoFencing() {

    val geoReq  = createGeoFenceList()

    geofencingClient.addGeofences(geoReq, pendingIntent).apply {
        addOnSuccessListener {
            Timber.e("added geofence!")
        }

        addOnFailureListener {
            Timber.e("geofence failed!")
        }
    }

}

private fun createGeoFenceList(): GeofencingRequest {

    val geofenceList = arrayListOf<Geofence>()


    geofenceList.add(Geofence.Builder().apply {
        setRequestId(task.id.toString())

        setCircularRegion(
                task.location.latitude,
                task.location.longitude,
                task.range.toFloat()
        )

            // Set the expiration duration of the geofence. This geofence gets automatically
            // removed after this period of time.
        setExpirationDuration(Geofence.NEVER_EXPIRE)
        setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
            // Create the geofence.

    }.build())


    
    return getGeofencingRequest(geofenceList)
}

private fun getGeofencingRequest(
    fenceList: List<Geofence>,
): GeofencingRequest {
    return GeofencingRequest.Builder().apply {
        setInitialTrigger(
            if(task.reminderCondition == ReminderCondition.ON_ENTRY)
                GeofencingRequest.INITIAL_TRIGGER_ENTER
            else
                GeofencingRequest.INITIAL_TRIGGER_EXIT)
        addGeofences(fenceList)
    }.build()}

我在这里添加地理围栏

fun createGeoFence(context: Context, task: Task){
val geofencePendingIntent: PendingIntent by lazy {
    task.range = 150.0
    val intent = Intent(context, ReminderNotificationBroadcastReceiver::class.java)

    intent.putExtra(TASK_ID, task.id)

    if(Build.VERSION.SDK_INT > 30){
        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }else{
        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }
}
val geoFencingHelper = GeoFencingHelper(context, task, geofencePendingIntent)

geoFencingHelper.initiateGeoFencing()}

这是在触发地理围栏事件时向用户显示通知的广播接收器。

class ReminderNotificationBroadcastReceiver @Inject constructor(private val dao: TaskDao): BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

    val builder = NotificationCompat.Builder(context, "2222")
        .setSmallIcon(R.drawable.ic_launcher_background)
        .setContentTitle("Reminder \"\".")
        .setContentText("Reminder \".")
        .setOngoing(true)
        .setColor(Color.BLUE)

    with(NotificationManagerCompat.from(context)) {
        notify(2222, builder.build())
    }}}

到目前为止我做过的事情 -

  1. 禁用电池优化。
  2. 后台位置权限一直被允许。

问题 -

我使用了 lockito 和 gps 模拟器等模拟定位应用程序来测试应用程序。 但是地理围栏事件没有被触发,我也尝试构建和测试 android codelab 提供的示例项目,但我在该应用程序中也面临同样的问题。

【问题讨论】:

    标签: android geolocation android-location geofencing android-geofence


    【解决方案1】:

    在 SO 和网上也有类似的问题,但简而言之,Google codelab 对此没有最新的。用于地理围栏的PendingIntent 必须是可变的才能工作。所以在你的情况下:

    fun createGeoFence(context: Context, task: Task){
    val geofencePendingIntent: PendingIntent by lazy {
        ....
    
        if(Build.VERSION.SDK_INT > 30){
            PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
        } else{
            PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        }
    }
    

    或者像这样没有if,因为PendingIntent 默认是可变的,直到Build.VERSION_CODES.R

    fun createGeoFence(context: Context, task: Task){
    val geofencePendingIntent: PendingIntent by lazy {
        ....
    
        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多