在意识API中获取栅栏无效

发布于 2025-01-23 11:18:49 字数 3962 浏览 0 评论 0原文

嗨,我是在应用程序中工作时必须在用户开始驾驶时通知的,我使用了Neura API,但它需要固定的通知,因此我正在使用意识API尝试。 我需要在AndroidManifest.xml中进行广播,因为我想触发该事件,即使该应用不在后台。 围栏的注册良好,广播是触发的,但我无法获得栅栏和弹性,我正在尝试不同的活动进行测试。

在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,这是当我拿到耳机围栏的事件时,我试图获得栅栏和fencestatus,但我得到了栅栏= null = null and 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)
   }
}

我注册了围栏,这里检查并接受了所有权限,寄存器的工作正常


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)
            }

Hi i´m working in app that have to notificate when the user starts driving, i used Neura Api but it needs a fixed notification, so i´m trying it with Awareness Api.
I need the broadcast in the AndroidManifest.xml because i want to trigger the event even the app is not in background.
The Fence is registered fine, the broadcast is triggered but i can´t get the fenceKey and fenceStatus , i´m trying with different events for test.

In the AndroidManifest.xml i added permissions, api key and declared the broadcast.

         <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" />

This is my FenceReceiver.kt, here is the problem when i got the event of headphones fence i try to get the fenceKey and fenceStatus but i got fenceKey = null and 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)
   }
}

Here is where i register the Fences, here all permissions are checked and accepted, the register is working good


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}")
        }
    }

}

If i do this method i can check that the fence is correctly registered

 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)
            }
    }

And if i do this i got the user activity correctly

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)
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2025-01-30 11:18:49

将未决的意图从 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/awareness/android-api/fence-register ,但对我来说,这确实做到了这一点。

Change the pending intent flag from FLAG_IMMUTABLE to FLAG_MUTABLE:

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

Unfortunately the documentation doesn't show how to create the mPendingIntent in https://developers.google.com/awareness/android-api/fence-register, but for me this did the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文