在开发后台服务的时候遇到如下报错
详细信息如下
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.coolweather, PID: 10459
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.
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.
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4725)
at android.app.ActivityThread.access$2000(ActivityThread.java:257)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2130)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7911)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
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.
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.
at android.app.PendingIntent.checkFlags(PendingIntent.java:378)
at android.app.PendingIntent.buildServicePendingIntent(PendingIntent.java:727)
at android.app.PendingIntent.getService(PendingIntent.java:689)
at com.example.coolweather.service.AutoUpdateService.onStartCommand(AutoUpdateService.java:46)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4707)
... 9 more
源代码:
public int onStartCommand(Intent intent, int flags, int startId)
{
updateWeather();
updateBingPic();
AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
int anHour = 8 * 60 * 60 * 1000;//这是8小时的毫秒数
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent i = new Intent(this,AutoUpdateService.class);
PendingIntent pi;
pi = PendingIntent.getService(this,0,i,0);
manager.cancel(pi);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
return super.onStartCommand(intent,flags,startId);
}
从错误信息来看,这是 Android API 31 中加入的一个新限制,如果没有使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 标记来创建 PendingIntent,就会抛出 IllegalArgumentException 异常。
查阅资料,Android 12之后创建的每个PendingIntent对象必须使用PendingIntent.FLAG_MUTABLE或PendingIntent.FLAG_IMMUTABLE标志指定可变性,以提高应用的安全性。
解决方案:
public int onStartCommand(Intent intent, int flags, int startId)
{
updateWeather();
updateBingPic();
AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
int anHour = 8 * 60 * 60 * 1000;//这是8小时的毫秒数
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent i = new Intent(this,AutoUpdateService.class);
PendingIntent pi;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pi = PendingIntent.getService(this,0,i,PendingIntent.FLAG_MUTABLE);
} else {
pi = PendingIntent.getService(this,0,i,0);
}
manager.cancel(pi);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
return super.onStartCommand(intent,flags,startId);
}