Android 警报管理器重复发出通知

Android 警报管理器重复发出通知,android,Android,我正在处理通知,输入日期时效果良好,但当我输入另一个日期时,报警管理器也会再次重复上一个日期。假设我输入了第一个日期,然后第二个日期,然后通知会显示第1个日期2次,如果我输入第3个日期,则会显示第1个日期3次,第2个日期两次。我正在从sqlite数据库中获取它并获取他们使用for循环。我知道我需要在for循环或报警管理器中进行修改。但是如何解决这个问题,因为我可以在不重复前一个循环的情况下正确获取日期 MainActivity.Class protected void onCreate(Bun

我正在处理通知,输入日期时效果良好,但当我输入另一个日期时,报警管理器也会再次重复上一个日期。假设我输入了第一个日期,然后第二个日期,然后通知会显示第1个日期2次,如果我输入第3个日期,则会显示第1个日期3次,第2个日期两次。我正在从sqlite数据库中获取它并获取他们使用for循环。我知道我需要在for循环或报警管理器中进行修改。但是如何解决这个问题,因为我可以在不重复前一个循环的情况下正确获取日期

MainActivity.Class

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            date = findViewById(R.id.dateET);
            name = findViewById(R.id.nameET);
            age = findViewById(R.id.ageET);
            address = findViewById(R.id.addressET);
            addStudent = findViewById(R.id.button);
            showStudents = findViewById(R.id.button1);
            sharedPreferences = getSharedPreferences("alarmRequestCode", 
            MODE_PRIVATE);
            requestCode = sharedPreferences.getInt("requestCodeValue", 1);
            databaseSource = new DatabaseSource(this);
            addStudent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
     Model model = new 
Model(name.getText().toString(),Integer.valueOf(age.getText().toString()), 
address.getText().toString(), date.getText().toString());

                    Boolean status = databaseSource.addStudent(model);

                    if (status) {

                        Toast.makeText(getApplicationContext(), "Added Successfully", 
Toast.LENGTH_LONG).show();
                        name.setText("");
                        age.setText("");          
                        address.setText("");

                    } else {

                        Toast.makeText(getApplicationContext(), "Error", 
Toast.LENGTH_LONG).show();
                    }

                    List<Model> list = new ArrayList<>();

                    list = databaseSource.getAllStudent();

                      //this loop is sending the previous date again to alarm manager
                    for (int i = 0; i < list.size(); i++) {

                        Model student = list.get(i);
                        String date = student.getDate();
                        setAlarm(date, student);

                    }

                }
            });

  showStudents.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, StudentList.class);
                    startActivity(intent);
               }
            });
           }
    private void setAlarm(String date, Model med) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy hh:mm aa");
    Calendar calendar = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
       try {
              calendar.setTime(sdf.parse(date));
                int dateForAlarm = calendar.get(Calendar.DAY_OF_MONTH);
                int monthForAlarm = calendar.get(Calendar.MONTH);
                int yearForAlarm = calendar.get(Calendar.YEAR);
                int hourForAlarm = calendar.get(Calendar.HOUR_OF_DAY);
                int minuteForAlarm = calendar.get(Calendar.MINUTE);
                int secondForAlarm = 0;

                cal.set(yearForAlarm, monthForAlarm, dateForAlarm, hourForAlarm, minuteForAlarm, 
secondForAlarm);


            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (Calendar.getInstance().getTimeInMillis() >= cal.getTimeInMillis()) {

                Log.d("before", "before current time");

                } else {

                AlarmManager alarmManager = (AlarmManager) 
getSystemService(Context.ALARM_SERVICE);
                Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
                intent.putExtra("medName", med.getName());
                intent.putExtra("med", "true");
                PendingIntent pendingIntent = 
PendingIntent.getBroadcast(getApplicationContext(), 
                requestCode, intent, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
                requestCode++;
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("requestCodeValue", requestCode);
                editor.commit();


            }
        }
    }
public class AlarmReceiver extends BroadcastReceiver {
int code = 1;
SharedPreferences sharedPreferences;
NotificationHelper helper;
@Override
public void onReceive(Context context, Intent intent) {
    sharedPreferences = context.getSharedPreferences("alarmRequestCode", MODE_PRIVATE);
    code = sharedPreferences.getInt("requestCodeValue", 1);
    String medName  = intent.getStringExtra("medName");
    String med = intent.getStringExtra("med");

    helper = new NotificationHelper(context);

    if ("true".equals(med) ){

        showNotification(medName,
                    "it's time to call " +medName +" at");

    }
}

private void showNotification(String title, String message) {
    NotificationCompat.Builder notification = helper.getChannelNotification(title,message);
    helper.getManager().notify(code, notification.build())
    code++;
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("requestCodeValue", code);
    editor.commit();
    }
    }
通知助手

public class NotificationHelper extends ContextWrapper {
    public static final String channelId = "channel ID";      
    private NotificationManager manager;
    public NotificationHelper(Context base) {
        super(base);
    }

    public NotificationManager getManager(){

        if (manager == null){

            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        }

        return manager;
    }

    public NotificationCompat.Builder getChannelNotification(String title, String message){

        return new NotificationCompat.Builder(getApplicationContext(), channelId)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                //.setContentIntent(activityIntent)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message)
                .setAutoCancel(true)
                .setOngoing(false);

                 }
                  }