簡體   English   中英

在 Awareness Api 中獲取 fenceKey null

[英]Getting fenceKey null in Awareness Api

嗨,我正在使用必須在用戶開始駕駛時通知的應用程序,我使用了 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)
            }

將掛起的意圖標志從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,但對我來說,這成功了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM