我有一个时间选择器来安排特定时间的警报,并且警报每1小时触发一次。我希望另一个时间选择器取消警报,如果当前时间与设置的取消时间匹配,则应该取消警报。我开发了设置警报和取消警报的功能。但是如何匹配日期和时间。我尝试过,但是我无法取消警报。 设置提醒功能
public static void setReminder(Context context, Class<?> cls, int hour, int min) //,int endhour,int endmin)
{
Date date1=new Date();
Date date2=new Date();
// long date1Long=0,date2Long=0;
Calendar calendar = Calendar.getInstance();
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR_OF_DAY, hour);
setcalendar.set(Calendar.MINUTE, min);
setcalendar.set(Calendar.SECOND, 0);
// cancel already scheduled reminders
cancelReminder(context,cls);
if(setcalendar.before(calendar))
setcalendar.add(Calendar.DATE,1);
// Enable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
// String s=Integer.toString(currentHourIn24Format);
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String currentDate = formatter1.format(calendar1.getTime());
// Date date=formatter1.format(currentDate);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
date.setHours(22);
date.setMinutes(30);
date.setSeconds(0);
String setdate=dateFormat.format(date);
// Date date = new Date();
// Calendar calendar2 = Calendar.getInstance();
// calendar2.setTime(date);
// calendar2.set(Calendar.HOUR_OF_DAY, endhour);// for 6 hour
// calendar2.set(Calendar.MINUTE, endmin);// for 0 min
// calendar2.set(Calendar.SECOND, 0);// for 0 sec
// String setdate= formatter1.format(calendar2.getTime());
try {
date1=formatter1.parse(currentDate);
date2=formatter1.parse(setdate);
} catch (ParseException e) {
e.printStackTrace();
}
if(date1.compareTo(date2)>0)
{
cancelReminder(context,cls);
Toast.makeText(context, "current date exceeds set date please add bed time", Toast.LENGTH_SHORT).show();
}
else
{
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
Toast.makeText(context,"Notification Alarm set",Toast.LENGTH_SHORT).show();
// cancelReminder(context,cls);
}
取消警报
/
/ Disable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
pendingIntent.cancel();