jackie Maik
jackie Maik

Reputation: 79

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:

java.lang.IllegalArgumentException: com.tonyapp.rabbitfarm: 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:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at com.allyants.notifyme.NotifyMe.scheduleNotification(NotifyMe.java:159)
        at com.allyants.notifyme.NotifyMe.<init>(NotifyMe.java:89)
        at com.allyants.notifyme.NotifyMe$Builder.build(NotifyMe.java:327)
        at com.tonyapp.rabbitfarm.NotesTakerActivity.onTimeSet(NotesTakerActivity.java:291)
        at com.wdullaer.materialdatetimepicker.time.TimePickerDialog.notifyOnDateListener(TimePickerDialog.java:1893)
        at com.wdullaer.materialdatetimepicker.time.TimePickerDialog.lambda$onCreateView$3$com-wdullaer-materialdatetimepicker-time-TimePickerDialog(TimePickerDialog.java:761)
        at com.wdullaer.materialdatetimepicker.time.TimePickerDialog$$ExternalSyntheticLambda3.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7441)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1202)
        at android.view.View.performClickInternal(View.java:7418)
        at android.view.View.access$3700(View.java:835)
        at android.view.View$PerformClick.run(View.java:28676)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        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:1003)

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.

Upvotes: 5

Views: 17756

Answers (1)

Urvish Shiroya
Urvish Shiroya

Reputation: 682

  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'
}

Upvotes: 15

Related Questions