这是我在其中放置警报的保存按钮。但问题是,如果我添加 1 个警报,它会很好用,但是如果我在最后一个警报触发之前的下一分钟添加一个警报,它会取消最后一个警报并更新到新警报,我尝试更改请求代码,但它对我不起作用
save = (Button)findViewById(R.id.save_rem);
save.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
title_txt = title.getText().toString();
description_txt = description.getText().toString();
id++;
addRemInDb a = new addRemInDb();
int v = a.insert(title_txt, hour, minute, mMonth, mDay, mYear, description_txt);
//a.deleteTable();
a.closeDB();
if(v == -1)
{
Toast.makeText(getApplication(), "Title & Time Must Not Be Same",Toast.LENGTH_LONG).show();
}
else
{
Calendar cal = Calendar.getInstance();
//Setting alarm to be triggered on specified date and time
cal.set(mYear, mMonth, mDay, hour, minute, seconds);
requestcode++; // Here request code is adding every time when i click in save button
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmIntent.putExtra("title", title_txt); //This is title for notification
alarmIntent.putExtra("note", description_txt); //This is Description for notification
alarmIntent.putExtra("rq", requestcode);
PendingIntent sender = PendingIntent.getService(getApplicationContext(), requestcode, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
finish();
}
}
});
这是我的 AlarmReciever 类
public class AlarmReceiver extends Service
{
@Override
public void onCreate()
{
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Bundle extras = intent.getExtras();
String title=extras.getString("title");
String note=extras.getString("note");
int requestcode = extras.getInt("rq");
NotificationManager nManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notify, "Reminder", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getService(getApplicationContext(), requestcode, new Intent(getApplicationContext(), AlarmReceiver.class), 0);
notification.setLatestEventInfo(getApplicationContext(), note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults = Notification.DEFAULT_SOUND;
nManger.notify(NOTIFICATION_ID, notification);
}