我正在创建一个简单的时间选择器对话框,当用户设置时间时,我将该时间存储在Calendar实例中。
public void onTimeSet(TimePicker timePicker, int i, int i1)
{
tpCalendar = Calendar.getInstance();
tpCalendar.set(Calendar.HOUR_OF_DAY, i);
tpCalendar.set(Calendar.MINUTE, i1);
tpCalendar.set(Calendar.SECOND,0);
updateReminder();
}
计算updateReminder()方法中的差异
private void updateReminder()
{
long currTime = System.currentTimeMillis();
long diff = tpCalendar.getTimeInMillis() - currTime;
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diff);
Intent reminderIntent = new Intent(this, ReminderReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, diff,
PendingIntent.getBroadcast(this, 1, reminderIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
}
基于差异我正在设置警报。我在Millis(diff变量)中得到了正确的差异。但问题是在设置警报后无论设置了什么差异,它都会在5秒后通知。
在广播接收器中,我只是创建一个通知。 我将不胜感激,谢谢。
答案 0 :(得分:3)
我做的事情就像我制作AlarmClock时(在TimePicked之后):
String[] arrTime = time.split(":");
int hour = Integer.parseInt(arrTime[0]);
int minute = Integer.parseInt(arrTime[1]);
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
calendar.set(GregorianCalendar.DATE, calendar.get(GregorianCalendar.DATE));
calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
calendar.set(GregorianCalendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("message", message);
PendingIntent pIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);