简体   繁体   中英

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

I am making Notification app and struggling with below error:

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, eg if it needs to be used with inline replies or bubbles.

I searched the solutions for this issue and there are no solution work for me.

below is my Mainactivity.java code:

 btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NotifyMe.cancel(getApplicationContext(),"test");
        }
    });


    btnNotify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dpd.show(getSupportFragmentManager(),"DatePickerDialog");
        }
    });
}

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

    now.set(Calendar.YEAR,year);
    now.set(Calendar.MONTH,monthOfYear);
    now.set(Calendar.DAY_OF_MONTH,dayOfMonth);

    tpd.show(getSupportFragmentManager(),"timepicker");



}

@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {


    now.set(Calendar.HOUR_OF_DAY,hourOfDay);
    now.set(Calendar.MINUTE,minute);
    now.set(Calendar.SECOND,second);



    NotifyMe notifyMe = new NotifyMe.Builder(getApplicationContext())
            .title(edt1.getText().toString())
            .content(edt2.getText().toString())
            .color(255,0,0,255)
            .led_color(255,255,255,255)
            .time(now)
            .addAction(new Intent(),"Snooze",false)
            .addAction(new Intent(),"Dismiss",true)
            .addAction(new Intent(),"Done")
            .large_icon(R.mipmap.ic_launcher_round)
            .build();

}

Please help me fix this problem. thanks in advance.

  1. Determine whether your project has pending intent.
  2. If Yes then search for PendingIntent in the whole project and add the flag to PendingIntent like this.
  • before
PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
  • after
PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_MUTABLE);

OR

If there is no PendingIntent in your project or you get an error even though you write the flag:

  • Add the following to your build.gradle(app) dependencies.
dependencies {
  // For Java
  implementation 'androidx.work:work-runtime:2.7.1' 

  // For Kotlin
  implementation 'androidx.work:work-runtime-ktx:2.7.1'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM