Android 12 以上PendingIntent使用注意FLAG_IMMUTABLE

Android 12 以上PendingIntent使用注意FLAG_IMMUTABLE_第1张图片

 

遇到如下报错:

Fatal Exception: java.langlllegalArgumentException

: Targeting S+ (version 31 and above) reures that one of FLAG_MMUTABLE r FLA-MUTABLE be specfed when creating a Pendinglntent. Strongly consider using FLAG_JMMUTABLE only use FLAG_MUTABLE if some unctionalty depends on the Pendinglintent being mutable e.g, ifitneeds to be used with inline replies or bubles

Android 12 以上PendingIntent使用注意FLAG_IMMUTABLE_第2张图片

 

 

关于PendingIntent

进行以下修改:

PendingIntent pendingIntent ;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
    pendingIntent = PendingIntent.getActivity(ControlActivity.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}else {
    pendingIntent = PendingIntent.getActivity(ControlActivity.this, 0, intent, 0);
}

PendingIntent alarmIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
}

你可能感兴趣的:(android,android)