【问题标题】:Getting fenceKey null in Awareness Api在 Awareness Api 中获取 fenceKey null
【发布时间】:2022-07-13 16:44:16
【问题描述】:

嗨,我正在使用必须在用户开始驾驶时发出通知的应用程序,我使用了 Neura Api,但它需要一个固定的通知,所以我正在尝试使用 Awareness Api。 我需要 AndroidManifest.xml 中的广播,因为即使应用程序不在后台,我也想触发事件。 Fence 注册良好,广播被触发,但我无法获取 fenceKey 和 fenceStatus ,我正在尝试使用不同的事件进行测试。

在 AndroidManifest.xml 中,我添加了权限、api 密钥并声明了广播

         <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
         <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

        <receiver
            android:name=".usescase.receivers.FenceReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.safycab.safycab.FENCE_RECEIVER_ACTION" />
            </intent-filter>
        </receiver>

        <meta-data
            android:name="com.google.android.awareness.API_KEY"
            android:value="@string/awareness_key" />

这是我的 FenceReceiver.kt,这是当我收到耳机围栏事件时的问题,我尝试获取 fenceKey 和 fenceStatus 但我得到了 fenceKey = null 和 fenceStatus = 0

class FenceReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val fenceState = FenceState.extract(intent)

        context.showLocalNotification("fence key " + fenceState.fenceKey + " fence state" + fenceState.currentState)
   }
}

这是我注册 Fences 的地方,在这里检查并接受所有权限,注册工作良好


class FenceApiUtils(var activity: BaseActivity<*, *>) {

    var drivingFence = DetectedActivityFence.starting(DetectedActivityFence.IN_VEHICLE)

    var walkingFence = DetectedActivityFence.starting(DetectedActivityFence.ON_FOOT)

    val headPhoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN)

    fun createFences() {
        val intent = Intent(activity, FenceReceiver::class.java)

        val pendingIntent = PendingIntent.getBroadcast(
            activity.applicationContext, 0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        Awareness.getFenceClient(activity).updateFences(
            FenceUpdateRequest.Builder()
                .addFence(VEHICLE_FENCE_KEY, drivingFence, pendingIntent)
                .addFence(WALKING_FENCE_KEY, walkingFence, pendingIntent)
                .addFence(HEADPHONE_FENCE, headPhoneFence, pendingIntent)
                .build()
        ).addOnSuccessListener {
            log("Fence was successfully registered.")
        }.addOnFailureListener {
            log("Fence could not be registered: ${it.message}")
        }
    }

}

如果我这样做,我可以检查栅栏是否正确注册

 fun queryFence(key: String) {
        Awareness.getFenceClient(requireActivity())
            .queryFences(FenceQueryRequest.forFences(listOf(key))).addOnSuccessListener {
                val map = it.fenceStateMap
                for (fenceKey in map.fenceKeys) {
                    val fenceState = map.getFenceState(fenceKey)
                    requireContext().showLocalNotification(
                        "Fence " + fenceKey + ": "
                                + fenceState?.currentState
                                + ", was="
                                + fenceState?.previousState
                    )
                }
            }.addOnFailureListener {
                log(it.message)
            }
    }

如果我这样做,我会正确获取用户活动

Awareness.getSnapshotClient(requireActivity()).detectedActivity.addOnSuccessListener {
                val act = it.activityRecognitionResult
                val dtc = act.mostProbableActivity
                val conf = dtc.confidence
                val activityStr = dtc.toString()
                requireContext().showLocalNotification("Activity: $activityStr, Confidence: $conf/100")
            }.addOnFailureListener {
                log(it.message)
                log(it.localizedMessage)
            }

【问题讨论】:

    标签: android kotlin google-awareness user-activity


    【解决方案1】:

    将待处理意图标志从 FLAG_IMMUTABLE 更改为 FLAG_MUTABLE

    val pendingIntent = PendingIntent.getBroadcast(
        activity.applicationContext, 0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )
    

    不幸的是,文档没有显示如何在 https://developers.google.com/awareness/android-api/fence-register 中创建 mPendingIntent,但对我来说,这成功了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多