Please note FLAG_IMMUTABLE when using PendingIntent on Android 12 or above

 

I encountered the following error:

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

 

 

About 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);
}

Guess you like

Origin blog.csdn.net/loveseal518/article/details/131301899