requestActivityTransitionUpdates never calls the registered BroadcastReceiver

醉酒当歌 提交于 2020-02-28 08:29:22

问题


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.

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

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

And also one to start listening:

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

    // ...
}

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? :)

Thanks!


回答1:


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

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.

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

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

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



来源:https://stackoverflow.com/questions/59562813/requestactivitytransitionupdates-never-calls-the-registered-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!