[解決済み] Android 12 以降の PendingIntent は FLAG_IMMUTABLE または FLAG_MUTABLE を強制的に増やす必要があります

Android13 への適応に関して次のエラーが報告されます。

java.lang.IllegalArgumentException: android: S+ (バージョン 31 以降) をターゲットとする場合は、PendingIntent の作成時に FLAG_IMMUTABLE または FLAG_MUTABLE のいずれかを指定する必要があります。

02-26 20:00:47.055 27821 27821 I System  : java.lang.IllegalArgumentException: android: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
02-26 20:00:47.055 27821 27821 I System  : Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.checkFlags(PendingIntent.java:404)
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:674)
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.getBroadcast(PendingIntent.java:661)
02-26 20:00:47.055 27821 27821 I System  :     at com.XX.server.ntp.SunmiNetworkTimeUpdateService.<init>(SunmiNetworkTimeUpdateService.java:148)
02-26 20:00:47.055 27821 27821 I System  :     at com.XX.server.SunmiSystemServer.<init>(SunmiSystemServer.java:55)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.startOtherServices(SystemServer.java:2414)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.run(SystemServer.java:944)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.main(SystemServer.java:654)
02-26 20:00:47.055 27821 27821 I System  :     at java.lang.reflect.Method.invoke(Native Method)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)
02-26 20:00:47.055 27821 27821 W SystemServer: ***********************************************
02-26 20:00:47.055 27821 27821 E SystemServer: BOOT FAILURE starting SunmiSystemServer
02-26 20:00:47.055 27821 27821 E SystemServer: java.lang.IllegalArgumentException: android: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
02-26 20:00:47.055 27821 27821 E SystemServer: Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.checkFlags(PendingIntent.java:404)
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:674)
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.getBroadcast(PendingIntent.java:661)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.XX.server.ntp.SunmiNetworkTimeUpdateService.<init>(SunmiNetworkTimeUpdateService.java:148)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.XX.server.SunmiSystemServer.<init>(SunmiSystemServer.java:55)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.startOtherServices(SystemServer.java:2414)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.run(SystemServer.java:944)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.main(SystemServer.java:654)
02-26 20:00:47.055 27821 27821 E SystemServer:     at java.lang.reflect.Method.invoke(Native Method)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)
02-26 20:00:47.056 27821 27821 D SystemServerTiming: StartPrintManager

修正方法:

   148         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
   149             mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, PendingIntent.FLAG_IMMUTABLE);
   150         }else{
   151             mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
   152         } 

参考:

アプリと SDK 開発必見 | Android12 適応ガイドの共有を推進 - Zhihu (zhihu.com)

おすすめ

転載: blog.csdn.net/burly/article/details/129296597