以 S+(版本 31 及更高版本)为目标需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

jac*_*aik 4 java notifications android push-notification android-studio

我正在制作通知应用程序并努力解决以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

我搜索了这个问题的解决方案,但没有适合我的解决方案。

下面是我的 Mainactivity.java 代码:

 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();

}
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。提前致谢。

小智 13

  1. 确定您的项目是否有待定意向。
  2. 如果Yes然后在整个项目中搜索 PendingIntent 并将标志添加到 PendingIntent 中,如下所示。
PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)
PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_MUTABLE);
Run Code Online (Sandbox Code Playgroud)

或者

如果您的项目中没有 PendingIntent 或者即使您编写了该标志也会收到错误:

  • 将以下内容添加到您的build.gradle(app)依赖项中。
dependencies {
  // For Java
  implementation 'androidx.work:work-runtime:2.7.1' 

  // For Kotlin
  implementation 'androidx.work:work-runtime-ktx:2.7.1'
}
Run Code Online (Sandbox Code Playgroud)

  • 我没有在我的项目中使用 PendingIntent,当我添加实现“androidx.work:work-runtime-ktx:2.7.1”时,它解决了我的问题/感谢帮助 (2认同)