首页 > 解决方案 > Android Wear Geofence 调用 addGeofences-method 返回异常:1000

问题描述

我在Fossil Falster 3上玩Wear OS(它正在运行API 28)。另外请知道我只是从 开始,所以下面的代码至少可以说是不好的。Kotlin

我想使用广播接收器创建一个地理围栏。

在我的MainActivity.kt我有以下内容:

 override fun onStart() {
    super.onStart()
    requestForegroundAndBackgroundLocationPermissions()

    val locations = arrayOf(Location("tgh", 58.798233, 11.123959))
    val geofences = locations.map {
        Geofence.Builder()
            .setRequestId(it.name)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setCircularRegion(it.latitude, it.longitude, 100F)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL)
            .setLoiteringDelay(10)
            .build()
    }
    val geofencingRequest = GeofencingRequest.Builder().apply {
        setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
        addGeofences(geofences)
    }.build()
    val geofencePendingIntent: PendingIntent by lazy {
        val intent = Intent(this, GeofenceReceiver::class.java)
        PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }

    val geofencingClient = LocationServices.getGeofencingClient(this)
    geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run {
        addOnFailureListener {
            // display error
                exception -> Log.d(TAG, "addOnFailureListener Exception: $exception")
        }
        addOnSuccessListener {
            // move on
            Log.d(TAG, "addOnSuccessListener, move on")
        }
    }
}

GeofenceReceiver同一个文件中:

class GeofenceReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val geofencingEvent = GeofencingEvent.fromIntent(intent)

        if (geofencingEvent.hasError()) {
            Log.d(TAG, "geofencingEvent.hasError()")
        } else {
            geofencingEvent.triggeringGeofences.forEach {
                val geofence = it.requestId
                Log.d(TAG, "location at: " + geofence)
            }
        }
    }
}

具有以下manifest代码段:

<receiver android:name=".GeofenceReceiver"
    android:enabled="true"
    android:exported="true"/>

差不多就是这样。当我运行代码时,addOnFailureListener会触发并打印错误消息并导致 1000 异常。 异常 1000 根据谷歌文档的意思GEOFENCE_NOT_AVAILABLE。我已将手表和手机上的定位功能启用到手机上的最高级别 ( Improve Location Accuracy is ON)。在手表上,我已设置使用手表和手机的位置(最高级别)。我仍然不断收到 1000 错误代码。

标签: androidkotlinerror-handlingwear-os

解决方案


推荐阅读