java.lang.IllegalArgumentException: com.example.coolweather: Targeting S+ (version 31 and above) req

╰+哭是因爲堅強的太久メ 2023-10-12 00:18 50阅读 0赞

在开发后台服务的时候遇到如下报错

b17886486fe144e0b36bfef412b90816.png

详细信息如下

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.coolweather, PID: 10459
  3. java.lang.RuntimeException: Unable to start service com.example.coolweather.service.AutoUpdateService@8d9abb2 with Intent { cmp=com.example.coolweather/.service.AutoUpdateService }: java.lang.IllegalArgumentException: com.example.coolweather: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
  4. 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.
  5. at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4725)
  6. at android.app.ActivityThread.access$2000(ActivityThread.java:257)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2130)
  8. at android.os.Handler.dispatchMessage(Handler.java:106)
  9. at android.os.Looper.loopOnce(Looper.java:201)
  10. at android.os.Looper.loop(Looper.java:288)
  11. at android.app.ActivityThread.main(ActivityThread.java:7911)
  12. at java.lang.reflect.Method.invoke(Native Method)
  13. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  14. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
  15. Caused by: java.lang.IllegalArgumentException: com.example.coolweather: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
  16. 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.
  17. at android.app.PendingIntent.checkFlags(PendingIntent.java:378)
  18. at android.app.PendingIntent.buildServicePendingIntent(PendingIntent.java:727)
  19. at android.app.PendingIntent.getService(PendingIntent.java:689)
  20. at com.example.coolweather.service.AutoUpdateService.onStartCommand(AutoUpdateService.java:46)
  21. at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4707)
  22. ... 9 more

源代码:

  1. public int onStartCommand(Intent intent, int flags, int startId)
  2. {
  3. updateWeather();
  4. updateBingPic();
  5. AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
  6. int anHour = 8 * 60 * 60 * 1000;//这是8小时的毫秒数
  7. long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
  8. Intent i = new Intent(this,AutoUpdateService.class);
  9. PendingIntent pi;
  10. pi = PendingIntent.getService(this,0,i,0);
  11. manager.cancel(pi);
  12. manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
  13. return super.onStartCommand(intent,flags,startId);
  14. }

从错误信息来看,这是 Android API 31 中加入的一个新限制,如果没有使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 标记来创建 PendingIntent,就会抛出 IllegalArgumentException 异常。

查阅资料,Android 12之后创建的每个PendingIntent对象必须使用PendingIntent.FLAG_MUTABLE或PendingIntent.FLAG_IMMUTABLE标志指定可变性,以提高应用的安全性。

解决方案:

  1. public int onStartCommand(Intent intent, int flags, int startId)
  2. {
  3. updateWeather();
  4. updateBingPic();
  5. AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
  6. int anHour = 8 * 60 * 60 * 1000;//这是8小时的毫秒数
  7. long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
  8. Intent i = new Intent(this,AutoUpdateService.class);
  9. PendingIntent pi;
  10. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
  11. pi = PendingIntent.getService(this,0,i,PendingIntent.FLAG_MUTABLE);
  12. } else {
  13. pi = PendingIntent.getService(this,0,i,0);
  14. }
  15. manager.cancel(pi);
  16. manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
  17. return super.onStartCommand(intent,flags,startId);
  18. }

发表评论

表情:
评论列表 (有 0 条评论,50人围观)

还没有评论,来说两句吧...

相关阅读