I'm registering Broadcast receiver to receive activity events from Android's ActivityRecognition api.
Registering Broadcast receiver in Manifest:
<receiver
android:name=".ActivityBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
<intent-filter>
<action android:name="my_action" />
</intent-filter>
</receiver>
Requesting activity updates:
val broadCastIntent = Intent(this, ActivityBroadcastReceiver::class.java)
ActivityRecognition.getClient(this)
.requestActivityUpdates(
100,
PendingIntent.getBroadcast(
this,
0,
broadCastIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
)
Inside Broadcast Receiver:
class ActivityBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
Toast.makeText(
context,
ActivityRecognitionResult.hasResult(intent).toString(),
Toast.LENGTH_SHORT
).show()
}
}
When activity change is recognized (for example: change from WALKING to STILL) onReceive gets triggered inside broadcast receiver but ActivityRecognitionResult.hasResult(intent) always returns false.
From Android 12, the
PendingIntentmust declare either the flagPendingIntent.IMMUTABLEor the flagPendingIntent.MUTABLE, and here we needPendingIntent.MUTABLE.Ref: https://developer.android.com/reference/android/app/PendingIntent#constants_1
Hence, try after replacing your current
PendingIntentdefinition with the below one: