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