gpt4 book ai didi

java - Android使用服务将通知设置为特定时间

转载 作者:行者123 更新时间:2023-12-02 05:58:54 26 4
gpt4 key购买 nike

我正在使用服务发出通知。但是我现在遇到的问题是通知发生在上午 11:00 和下午,尽管我希望通知只发生在上午 11:00。

下面这个方法是我设置时间的方法。而且我想知道当我在不同的类(class)设置时间时,我的服务类如何知道设置的时间。而且这个方法甚至没有从我的服务类中调用。

private void setReminder(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
String time = sdf.format(calendar.getTime());
SavingData.setReminder(time, true);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent();
intent.setClass(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, myID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

最佳答案

这是我用来每天通知用户的方式之一:

Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR_OF_DAY, 11); //24-hour format
firingCal.set(Calendar.MINUTE, 00);
firingCal.set(Calendar.SECOND, 00);

long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();

if(intendedTime >= currentTime)
{
//this will set the alarm for current day if time is below 11 am
alarmManager.setRepeating(AlarmManager.RTC, intendedTime , AlarmManager.INTERVAL_DAY, pendingIntent);
}
else {
//this will set the alarm for the next day
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime ,
AlarmManager.INTERVAL_DAY, pendingIntent);
}

关于java - Android使用服务将通知设置为特定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836971/

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