时间选择器当前时间与时间选择器时间之间的差异 Android 警报

Time Picker Difference between current and Time Picker Time Android Alarm

我正在创建一个简单的时间选择器对话框,当用户设置时间时,我将该时间存储在日历实例中。

 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(差异变量)中得到正确的差异。但是问题是设置闹钟后不管设置什么差值,都是5秒后通知

在广播接收器中,我只是创建了一个通知。 我将不胜感激任何帮助,谢谢。

我在做 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);