Android ActivityRecognition and IMMUTABLE PendingIntent
Asked Answered
B

1

5

I am working with Android ActivityRecognition api. When setting the PendingIntent flag to be PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE (as recommended?) the received intent doesn't contains any extras.

Here is how I use it:

First, I create a pending intent like this:

private val intent: Intent = Intent(context, ActivityRecognitionReceiver::class.java) 
private val mActivityTransitionPendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) //This gives warning that Mutability flag required

Then, I set transitions list like this:

private val INTERESTING_TRANSITION = intArrayOf(
    DetectedActivity.IN_VEHICLE,
    DetectedActivity.STILL,
    DetectedActivity.ON_FOOT,
    DetectedActivity.WALKING,
 )

Next, creating a request:

val transitions = mutableListOf<ActivityTransition>()

   for (activity in INTERESTING_TRANSITION) {
        transitions.add(
            ActivityTransition.Builder()
                .setActivityType(activity)
                .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                .build()
        )
        transitions.add(
            ActivityTransition.Builder()
                .setActivityType(activity)
                .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                .build()
        )
    }

val request = ActivityTransitionRequest(transitions)

finally register the request:

   val transitionTask: Task<Void> = mActivityRecognitionClient.requestActivityTransitionUpdates(request, mActivityTransitionPendingIntent)

Now as you see, i use a broadcast receiver which should receive the intents of the ActivityRecognition broadcasts! Now the receiver do works!! It does receives the broadcasts as it should, but the extras are seems to be null!

The onReceive looks like this:

   override fun onReceive(context: Context, intent: Intent?) {
    Log.i(TAG, "onReceive:  ${intent?.extras}") //Here i am getting null!! 
    intent?.let {
        if(ActivityTransitionResult.hasResult(it)){
            Log.i(TAG, "onReceive:  hasTransition..")
            activityDetectionProcessor.onActivityTransition(ActivityTransitionResult.extractResult(it))
        }

        if (ActivityRecognitionResult.hasResult(it)) {
            Log.i(TAG, "onReceive: hasResult: ${it.toString()} ")
            processDetectedActivities(ActivityRecognitionResult.extractResult(it)?.probableActivities)
        }
    }
}

When I use only PendingIntent.FLAG_UPDATE_CURRENT it do works!! But then I am getting compile warnings that I should specify Mutability flag. When using PendingIntent.FLAG_MUTABLE it works as well, but it also gives warning (which isn't clear) in the code..

The target SDK is 31. All permissions has been given properly.

Update with image:

Please notice the compilation error i am getting, it is very strange: enter image description here

In addition, the compilation error is gone once i set the flags according to Build.VERSION like this:

 private val pIntentFlags = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
     PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} else {
    PendingIntent.FLAG_UPDATE_CURRENT
}
private val mActivityTransitionPendingIntent = PendingIntent.getBroadcast(context, 1, intent, pIntentFlags)
Backbite answered 6/11, 2021 at 7:41 Comment(0)
G
6

You have to add the PendingIntent.FLAG_MUTABLE flag when creating the PendingIntent for requestActivityTransitionUpdates().

I don't see any compilation errors or warnings when using PendingIntent.FLAG_MUTABLE with my code. If you do, please post the warning message.

Gaudreau answered 29/11, 2021 at 11:41 Comment(5)
Please refer to my update regarding the compilation error and how it was gone. My app minimum SDK is 23Backbite
The FLAG_MUTABLE flag was introduced since Android S so only using it inside the if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) block is common sense.Gaudreau
Right, well it was more a linting warning, not a compilation error.Backbite
Thanks followed this solution and was able to receive updates again (I was getting null like the Question owner) I have added the following check val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { PendingIntent.FLAG_MUTABLE } else { PendingIntent.FLAG_UPDATE_CURRENT }Cerated
Literally spend hours wasting and not understanding while EXTRAS are null until found this answer.Garvey

© 2022 - 2024 — McMap. All rights reserved.