requestActivityTransitionUpdates永远不会调用已注册的BroadcastReceiver [英] requestActivityTransitionUpdates never calls the registered BroadcastReceiver

查看:142
本文介绍了requestActivityTransitionUpdates永远不会调用已注册的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的应用程序,该应用程序可以测量android设备(Wifi,BT等)的所有可用传感器.其中之一是用户活动(通过ActivityRecognition API),但我无法使其正常运行.

I am coding a simple app that measures all available sensors of the android device (Wifi, BT, etc). One of them is the user activity (via ActivityRecognition API), but I can't make it works properly.

我编写了一个类来完成与用户活动有关的所有事情.我只想获取4个状态和一个属性来存储当前状态:

I code a class to do everything related to user activity. I want to get only 4 states and one attribute to store the current one:

var VEHICLE = "vehicle"
var WALKING = "walking"
var STILL = "still"
var UNKNOWN = "unknown"

private var current: String? = null

它还包含一个BroadcastReceiver对象,以处理活动转换:

It also includes a BroadcastReceiver object to handle activity transitions:

private var recognitionHandler = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            val result = ActivityRecognitionResult.extractResult(intent)

            val activity = result.mostProbableActivity
            current = when(activity.type) {
                DetectedActivity.IN_VEHICLE,
                DetectedActivity.ON_BICYCLE -> VEHICLE
                DetectedActivity.WALKING,
                DetectedActivity.RUNNING -> WALKING
                DetectedActivity.STILL -> STILL
                else -> UNKNOWN
            }
        }
    }
}

该类还具有两种定义意图和请求的方法:

The class also have two methods to define the intent and request:

private fun createIntent() : PendingIntent {
    val intent = Intent(context, recognitionHandler.javaClass)
    val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)

    context.registerReceiver(recognitionHandler, IntentFilter())
    return pendingIntent
}

private fun createRequest() : ActivityTransitionRequest {
    val types = listOf(
        DetectedActivity.IN_VEHICLE,
        DetectedActivity.WALKING,
        DetectedActivity.RUNNING,
        DetectedActivity.ON_BICYCLE,
        DetectedActivity.STILL
    )

    val transitions = mutableListOf<ActivityTransition>()
    types.forEach { activity ->
        transitions.add(
            ActivityTransition.Builder()
                .setActivityType(activity)
                .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                .build()
        )
    }

    return ActivityTransitionRequest(transitions)
}

也是开始聆听的人:

override fun start(onResult: (res: String?) -> Unit) {
    // ...

    intent = createIntent()
    val request = createRequest()
    ActivityRecognition.getClient(context)
        .requestActivityTransitionUpdates(request, intent)
            .addOnSuccessListener {
                Log.d("UserActivity Service info", "listening...")
            }
            .addOnFailureListener { e ->
                Log.d("UserActivity Service error", e.toString())
            }

    // ...
}

问题在于current属性始终为null.我认为我在意图或处理程序注册方面存在一些问题,但是我不知道在哪里.

The problem is that the current attribute is always null. I think I have some issues with intent or handler registration, but I have no idea where.

有人有任何评论吗? :)

Does someone have any comments? :)

谢谢!

推荐答案

这是您的问题.在来自createIntent()的这段代码中:

This is your problem. In this code from createIntent():

val intent = Intent(context, recognitionHandler.javaClass)
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)

context.registerReceiver(recognitionHandler, IntentFilter())
return pendingIntent

您返回在调用requestActivityTransitionUpdates()时使用的PendingIntent.但是,PendingIntent指的是动态创建的内部类(您的BroadcastReceiver),而Android无法实例化该类.

You return a PendingIntent that you use in the call to requestActivityTransitionUpdates(). However, that PendingIntent refers to a dynamically created inner class (your BroadcastReceiver) and Android cannot instantiate that class.

您还另外呼叫了registerReceiver(),但是在该呼叫中传递了 IntentFilter,因此从未调用已注册的BroadcastReceiver.

You also additionally call registerReceiver(), however you pass an empty IntentFilter in that call so the registered BroadcastReceiver is never called.

要解决此问题,可以提供与PendingIntent相匹配的正确IntentFilter,也可以将BroadcastReceiver重构为适当的类(而不是私有内部类),并确保已添加BroadcastReceiver显示在您的清单上,并使其公开可用(exported="true").

To fix the problem, you can either provide a correctIntentFilter that matches your PendingIntent OR you can refactor your BroadcastReceiver into a proper class (not a private inner class) and make sure that you've added the BroadcastReceiver to your manifest and make it publicly available (exported="true").

以下是使用BroadcastReceiver的方法的示例:

Here's an example of how to do this using a BroadcastReceiver:

https://steemit.com/utopian-io/@betheleyo/implementing-android-s-new-activity-recognition-transition-api

这篇关于requestActivityTransitionUpdates永远不会调用已注册的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆