使用Reminders
从用户处获取两个日期,第一个Start
提醒日期和第二个End
提醒日期
我successfully
为start
提醒了一个代码,repeat
提醒,现在我想知道如何在结束日期时删除提醒来自数据库?
我知道,我必须使用以下内容cancel an alarm
(从删除提醒数据库),但不知道在哪里使用相同的代码删除基于结束日期的提醒提供提醒,点击link查看活动代码
for (int i = IDmap.size(); i >= 0; i--) {
int id = IDmap.get(i);
// Get reminder from reminder database using id
Reminder temp = rb.getReminder(id);
// Delete reminder
rb.deleteReminder(temp);
// Delete reminder alarm
mAlarmReceiver.cancelAlarm(getApplicationContext(), id);
}
AlarmReceiver.java:
public class AlarmReceiver extends WakefulBroadcastReceiver {
AlarmManager mAlarmManager;
PendingIntent mPendingIntent;
@Override
public void onReceive(Context context, Intent intent) {
int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));
// Get notification title from Reminder Database
ReminderDatabase rb = new ReminderDatabase(context);
Reminder reminder = rb.getReminder(mReceivedID);
String mTitle = reminder.getTitle();
// Create intent to open ReminderEditOldActivity on notification click
Intent editIntent = new Intent(context, ReminderEditActivity.class);
editIntent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_alarm_on_white_24dp)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setTicker(mTitle)
.setContentText(mTitle)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(mClick)
.setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(mReceivedID, mBuilder.build());
}
public void setAlarm(Context context, Calendar calendar, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Calculate notification time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
// Start alarm using notification time
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + diffTime,
mPendingIntent);
// Restart alarm if device is rebooted
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Calculate notification timein
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
// ensure the next alarm is in the future
while(diffTime < 0) {
diffTime += RepeatTime;
}
// Start alarm using initial notification time and repeat interval time
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + diffTime,
RepeatTime , mPendingIntent);
// Restart alarm if device is rebooted
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancelAlarm(Context context, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Cancel Alarm using Reminder ID
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
mAlarmManager.cancel(mPendingIntent);
// Disable alarm
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
private String mTitle;
private String mTime;
private String mDate;
private String mRepeatNo;
private String mRepeatType;
private String mActive;
private String mRepeat;
private String[] mDateSplit;
private String[] mTimeSplit;
private int mYear, mMonth, mHour, mMinute, mDay, mReceivedID, mEndDay, mEndMonth, mEndYear;
private long mRepeatTime;
private String mEndDate;
private String[] mEndDateSplit;
private Calendar mCalendar, mEndCalendar;
private AlarmReceiver mAlarmReceiver;
// Constant values in milliseconds
private static final long milMinute = 60000L;
private static final long milHour = 3600000L;
private static final long milDay = 86400000L;
private static final long milWeek = 604800000L;
private static final long milMonth = 2592000000L;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
ReminderDatabase rb = new ReminderDatabase(context);
mCalendar = Calendar.getInstance();
mEndCalendar = Calendar.getInstance();
mAlarmReceiver = new AlarmReceiver();
List<Reminder> reminders = rb.getAllReminders();
for (Reminder rm : reminders) {
mReceivedID = rm.getID();
mRepeat = rm.getRepeat();
mRepeatNo = rm.getRepeatNo();
mRepeatType = rm.getRepeatType();
mActive = rm.getActive();
mDate = rm.getDate();
mTime = rm.getTime();
mEndDate = rm.getmEndDate();
mDateSplit = mDate.split("/");
mTimeSplit = mTime.split(":");
mEndDateSplit = mEndDate.split("/");
mDay = Integer.parseInt(mDateSplit[0]);
mMonth = Integer.parseInt(mDateSplit[1]);
mYear = Integer.parseInt(mDateSplit[2]);
mEndDay = Integer.parseInt(mEndDateSplit[0]);
mEndMonth = Integer.parseInt(mEndDateSplit[1]);
mEndYear = Integer.parseInt(mEndDateSplit[2]);
mHour = Integer.parseInt(mTimeSplit[0]);
mMinute = Integer.parseInt(mTimeSplit[1]);
mCalendar.set(Calendar.MONTH, --mMonth);
mCalendar.set(Calendar.YEAR, mYear);
mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mCalendar.set(Calendar.MINUTE, mMinute);
mCalendar.set(Calendar.SECOND, 0);
mEndCalendar.set(Calendar.MONTH, --mEndMonth);
mEndCalendar.set(Calendar.YEAR, mEndYear);
mEndCalendar.set(Calendar.DAY_OF_MONTH, mEndDay);
mEndCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mEndCalendar.set(Calendar.MINUTE, mMinute);
mEndCalendar.set(Calendar.SECOND, 0);
// Cancel existing notification of the reminder by using its ID
// mAlarmReceiver.cancelAlarm(context, mReceivedID);
// Check repeat type
if (mRepeatType.equals("Daily")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 2 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 3 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Weekly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
} else if (mRepeatType.equals("Monthly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}
// Create a new notification
if (mActive.equals("true")) {
if (mRepeat.equals("true")) {
mAlarmReceiver.setRepeatAlarm(context, mCalendar, mReceivedID, mRepeatTime);
} else if (mRepeat.equals("false")) {
mAlarmReceiver.setAlarm(context, mCalendar, mReceivedID);
}
}
}
}
}
}
已更新
@Override
public void onReceive(Context context, Intent intent) {
int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));
// Get notification title from Reminder Database
ReminderDatabase rb = new ReminderDatabase(context);
Reminder reminder = rb.getReminder(mReceivedID);
String[] s = mEndDateSplit = mEndDate.split("/");
int mEndDay = Integer.parseInt(mEndDateSplit[0]);
int mEndMonth = Integer.parseInt(mEndDateSplit[1]);
int mEndYear = Integer.parseInt(mEndDateSplit[2]);
int mHour = Integer.parseInt(mTimeSplit[0]);
int mMinute = Integer.parseInt(mTimeSplit[1]);
Calendar mEndCalendar = Calendar.getInstance();
mEndCalendar.set(Calendar.MONTH, --mEndMonth);
mEndCalendar.set(Calendar.YEAR, mEndYear);
mEndCalendar.set(Calendar.DAY_OF_MONTH, mEndDay);
mEndCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mEndCalendar.set(Calendar.MINUTE, mMinute);
mEndCalendar.set(Calendar.SECOND, 0);
String mRepeatNo = reminder.getRepeatNo();
if (mRepeatType.equals("Daily")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 2 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 3 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Weekly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
} else if (mRepeatType.equals("Monthly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}
if(SystemClock.elapsedRealtime() < mEndCalendar.getTimeInMillis()) {
String mTitle = reminder.getTitle();
// Create intent to open ReminderEditOldActivity on notification click
Intent editIntent = new Intent(context, ReminderEditActivity.class);
editIntent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_alarm_on_white_24dp)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setTicker(mTitle)
.setContentText(mTitle)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(mClick)
.setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(mReceivedID, mBuilder.build());
}
if (reminder.getRepeat().equals("true")) {
if( SystemClock.elapsedRealtime() + mRepeatTime > mEndCalendar.getTimeInMillis()){
// The next alert will happen after the end time, cancel it and delete it from the database.
cancelAlarm(context, mReceivedID);
rb.deleteReminder(reminder);
}
} else {
// The alert isn't set to be repeated, cancel and delete it from the database.
cancelAlarm(context, mReceivedID);
rb.deleteReminder(reminder);
}
}
答案 0 :(得分:0)
以下是我发表的评论中的意思。
public class AlarmReceiver extends WakefulBroadcastReceiver {
...
@Override
public void onReceive(Context context, Intent intent) {
ReminderDatabase rb = new ReminderDatabase(context);
Reminder reminder = rb.getReminder(mReceivedID);
.......
if(rm.getRepeat().equals("true")){
String[] s = mEndDateSplit = mEndDate.split("/");
int mEndDay = Integer.parseInt(mEndDateSplit[0]);
int mEndMonth = Integer.parseInt(mEndDateSplit[1]);
int mEndYear = Integer.parseInt(mEndDateSplit[2]);
int mHour = Integer.parseInt(mTimeSplit[0]);
int mMinute = Integer.parseInt(mTimeSplit[1]);
Calendar mEndCalendar = Calendar.getInstance();
mEndCalendar.set(Calendar.MONTH, --mEndMonth);
mEndCalendar.set(Calendar.YEAR, mEndYear);
mEndCalendar.set(Calendar.DAY_OF_MONTH, mEndDay);
mEndCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mEndCalendar.set(Calendar.MINUTE, mMinute);
mEndCalendar.set(Calendar.SECOND, 0);
String mRepeatNo = rm.getRepeatNo();
if (mRepeatType.equals("Daily")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 2 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Every 3 Days")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Weekly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
} else if (mRepeatType.equals("Monthly")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}
if(SystemClock.elapsedRealtime() + mRepeatTime > nEndCalendar.getTimeInMills()) {
// The next alert will happen after the end time, cancel it and delete it from the database.
cancelAlarm(context, mReceivedID);
rb.deleteReminder(mReceivedID);
}
}else{
// The alert isn't set to be repeated, cancel and delete it from the database.
cancelAlarm(context, mReceivedID);
rb.deleteReminder(mReceivedID);
}
}
}