androidkotlinandroid-intentandroid-activityactivity-recognition

Successfully registered a BroadcastReceiver to requestActivityTransitionUpdates, but onReceive never called


I am currently working on an app that needs to know whether or not the user is currently still or not. I have followed the codelab tutorial from Android (as well as other tutorials): https://developer.android.com/codelabs/activity-recognition-transition#0

I have added the permissions required in the manifest file (and accepted them. The settings show that my app is allowed to use activity): manifest file

I have created a class that inherits from BroadcastReceiver and implemented a rudimentary version of onReceive to see if it works:

class ActivityController : BroadcastReceiver() {
val transitions = mutableListOf<ActivityTransition>()

companion object {
    const val REQUEST_CODE_INTENT_ACTIVITY_TRANSITION = 122
}

init {
    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.IN_VEHICLE)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.IN_VEHICLE)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.WALKING)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.WALKING)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.STILL)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.STILL)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.RUNNING)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
            .build()

    transitions +=
        ActivityTransition.Builder()
            .setActivityType(DetectedActivity.RUNNING)
            .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
            .build()
}

override fun onReceive(p0: Context?, p1: Intent?) {
    Log.d("proj", "please work")
    Toast.makeText(MainActivity.instance()!!, "please work", Toast.LENGTH_LONG).show()
}
}

In my MainActivity.kt's onStart method I have the following code, creating an instance of the ActivityController, registering it as a receiver and requesting activity transition updates:

override fun onStart() {
    super.onStart()

    activityController = ActivityController()
    registerReceiver(activityController, IntentFilter(TRANSITIONS_RECEIVER_ACTION))
    val request = ActivityTransitionRequest(activityController.transitions)

    ActivityRecognition.getClient(this)
        .requestActivityTransitionUpdates(request, getPendingIntent())
        .addOnSuccessListener { Log.d("proj", "Request success") }
        .addOnFailureListener { Log.d("proj", "Request failure") }
}

private fun getPendingIntent(): PendingIntent {
    val intent = Intent(TRANSITIONS_RECEIVER_ACTION, null, this, ActivityController::class.java)
    return PendingIntent.getBroadcast(this, 0, intent, 0)
}

I can see that the request is successful: successful request but the onReceive function is never called.

This has been driving me nuts the entire day, so any help would be appreciated :-)


Solution

  • Okay, so I played around with it some more and got it working somewhat (the p1: Intent? parameter always returns null for some reason, but now onReceive is actually called). I found out that the change in activity is too slow for my purpose (30-60 second range), but here is what I did to come to this point, if anyone sees this post:

    I followed Foodie Dev's tutorial: https://www.youtube.com/watch?v=2Em5ep_KYIY&ab_channel=FoodieDev

    I realised that I didn't include a 'receiver' clause inside of my 'activity' clause in the Manifest file: receiver in manifest

    I think that's all in all what I did besides removing the registerReceiver(...) in onCreate of MainActivity.jt, and changing the structure of my code a little (like accessing the client in the onCreate method of MainActivity.kt instead of onStart)

    Hope this helps if anyone finds themselves in the same situation.