gpt4 book ai didi

android - 时间选择器当前时间和时间选择器时间之间的差异 Android 闹钟

转载 作者:行者123 更新时间:2023-11-29 17:15:52 25 4
gpt4 key购买 nike

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

 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);

关于android - 时间选择器当前时间和时间选择器时间之间的差异 Android 闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com