[Android Studio] 解決Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent
在Android 12 (含)以上
使用PendingIntent時,若沒有加入參數中加入PendingIntent.FLAG_IMMUTABLE ,在執行時會發生錯誤:
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent
如果要解決這個問題,就需要在參數中加入PendingIntent.FLAG_IMMUTABLE這個falg
example:
原本為
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 2, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
修改為
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 2, intentAlarm, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
這樣在Android 12執行時,錯誤就不見了~
參考 reference:
https://www.bswen.com/2022/06/how-to-solve-pendingintent-error.html
留言
張貼留言