Android targeting s+ (version 31 and above) requires that one of flag_immutable or flag_mutable be specified when creating a pendingintent.

passยท2023๋…„ 2์›” 22์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
4/41

targeting s+ (version 31 and above) requires that one of flag_immutable or flag_mutable be specified when creating a pendingintent.

๐Ÿค” ์ด ์˜ค๋ฅ˜๋Š” Android์—์„œ kotlin์œผ๋กœ AlarmManager๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•˜์—ฌ PendingIntent๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋ฐœ์ƒํ•˜์˜€๋‹ค.


์œ„ ๋ฌธ์žฅ์„ ๊ทธ๋Œ€๋กœ ํ•ด์„ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
PendingIntent๋ฅผ ์ƒ์„ฑํ•  ๋•Œ s+(๋ฒ„์ „ 31 ์ด์ƒ)๋ฅผ ํƒ€๊ฒŸํŒ…ํ•˜๋ ค๋ฉด flag_immutable ๋˜๋Š” flag_mutable ์ค‘ ํ•˜๋‚˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ค๋ฅ˜์˜ ์ถ”๊ฐ€ ๋‚ด์šฉ์ด ์žˆ๋Š”๋ฐ ๊ฐ„๋‹จํžˆ ๋œป์„ ํ•ด์„ํ•ด๋ณด๋ฉด Google ๋ฌธ์„œ์—์„œ๋Š” ์ผ๋ถ€ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•œ PendingIntent์— ์˜์กดํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ FLAG_MUTABLE์„ ์‚ฌ์šฉํ•˜๊ณ , ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” FLAG_IMMUTABLE ์‚ฌ์šฉ์„ ๊ฐ•๋ ฅํžˆ ๊ถŒ๊ณ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.


PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

๊ทธ๋ž˜์„œ PendingIntent์— ์žˆ๋Š” getBroadcast ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, flags ๋ถ€๋ถ„์„ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ , ํ•ด๊ฒฐ๋ฐฉ์•ˆ์œผ๋กœ s+(๋ฒ„์ „ 31 ์ด์ƒ)์˜ ๊ฒฝ์šฐ์—๋Š” IMMUTABLE ํ˜น์€ MUTABLE์„ ์ถ”๊ฐ€ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์˜€๋‹ค.



val ALARM_FLAG = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
    PendingIntent.FLAG_UPDATE_CURRENT
}
        
val pendingIntent = PendingIntent.getBroadcast(
    this,
    id,
    this, AlarmReceiver::class.java),
    ALARM_FLAG
)
profile
์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€