Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestActivityTransitionUpdates never calls the registered BroadcastReceiver

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!

like image 596
lucasmenendez Avatar asked Nov 19 '25 12:11

lucasmenendez


1 Answers

I struggled with this for awhile. I was finally able to get it to work by using this PendingIntent:

PendingIntent.getBroadcast(mContext, 0, new Intent("ACTION_PROCESS_ACTIVITY_TRANSITIONS"), Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);

The flags at the end are important.

like image 104
Kris B Avatar answered Nov 22 '25 02:11

Kris B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!