Android 12 以上PendingIntent使用注意FLAG_IMMUTABLE

遇到如下报错:

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

关于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);
}

猜你喜欢

转载自blog.csdn.net/loveseal518/article/details/131301899