Android Geofencing API无法执行我的PendingIntent

问题描述

我试图使用Android上的Google Geofencing API设置地理围栏。我创建了broadcastReceiver来处理过渡事件,并使用显式意图创建了待处理意图,该意图在创建地理围栏时传递给了API。但是,我的接收者的onReceive从未被调用。我曾尝试使用setNotificationresponsivenesssetinitialTrigger的不同设置,但没有任何帮助。我已经在物理设备(带有Android 10的诺基亚7.1)和带有Android 10和6的两个模拟器上进行了尝试,但在这两个设备上均无法使用。我尝试等待几分钟以触发事件,但从未成功。我还尝试了不同的半径值,没有结果。 在Android 10上,我可以在系统进程日志中看到地理围栏确实已注册,但随后我只能看到一些有关位置可用性的条目,例如这样。即使isLocationAvailable为真(并且我不知道该依赖什么),接收方仍然不会被触发:

I/GeofencerStateMachine: sendNewLocationAvailability: availability=LocationAvailability[isLocationAvailable: false]

这是一个不起作用的最小代码示例:

GeofenceReceiver.kt

class GeofenceReceiver : broadcastReceiver() {

    companion object {
        fun makePendingIntent(context: Context) = PendingIntent.getbroadcast(
            context,1239,Intent(context,GeofenceReceiver::class.java),PendingIntent.FLAG_UPDATE_CURRENT
        )
    }

    override fun onReceive(context: Context,intent: Intent) {
        Log.e("geofencereceiver","received event")
        // some code
    }
}

AndroidManifest.xml(忽略了不相关的部分)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.patlejch.geofenceminimal">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <application ...>
        ....
        <receiver android:name=".GeofenceReceiver" />
        <!-- note that setting the receiver as exported makes no difference (and it shouldn't) -->
    </application>

</manifest>

注册地理围栏(this是一项活动)。执行此代码后,该应用程序已被授予ACCESS_FINE_LOCATIONACCESS_BACKGROUND_LOCATION

// googleplex mountain view
val latitude = 37.421912
val longitude = -122.084068
val radius = 250.0f

val geofenceGms = Geofence.Builder()
    .setRequestId("test_geofence")
    .setCircularRegion(latitude,longitude,radius)
    .setExpirationDuration(Geofence.NEVER_EXPIRE)
    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
    //.setNotificationresponsiveness(...)
    .build()

val request = GeofencingRequest.Builder()
    //.setinitialTrigger(Geofence.GEOFENCE_TRANSITION_EXIT)
    .addGeofence(geofenceGms)
    .build()

val geofencingClient = LocationServices.getGeofencingClient(this)
try {
    geofencingClient.addGeofences(request,GeofenceReceiver.makePendingIntent(this))
        .addOnSuccessListener {
            Toast.makeText(this,"registered",Toast.LENGTH_SHORT).show()
        }
        .addOnFailureListener {
            Toast.makeText(this,"failure",Toast.LENGTH_SHORT).show()
        }
} catch (e: SecurityException) {}

有人成功解决过类似的问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...