我正在处理通知,在输入日期时效果很好,但是当我输入另一个日期时,警报管理器也会再次重复上一个日期。假设我输入了第一个日期,则第二个日期,然后通知显示了第一个日期2次,如果我输入3rd日期,它将显示1st 1 3次和2nd 1 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();
}
}
}
AlarmReceiver.Class
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();
}
}
NotificationHelper
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);
}
}