[英]Android Set notification to specific time using service
I am using services to make notifications. 我正在使用服务进行通知。 But the problem I have right now is that the notifications happens at 11:00 AM & PM although I want the notification only happen at 11:00 AM.
但我现在遇到的问题是通知发生在上午11点和下午12点,虽然我希望通知只发生在上午11点。
This method below is how I set the time. 下面的方法是我如何设置时间。 And I wonder how does my service class know what time is set when I set the time on a different class.
我想知道当我在不同的课程上设置时间时,我的服务类如何知道设置的时间。 And this method is not even called from my service 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);
}
This is one of the ways I have used to notify the user on a daily basis: 这是我用来每天通知用户的方法之一:
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);
}
This will set an alarm at 11.am 这将在上午11点发出警报
PendingIntent pintent2 = PendingIntent.getBroadcast(context, 0, intentService2, 0);
AlarmManager alarm2 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
int INTERVAL_DAY1 = 24 * 60 * 60 * 1000;
Calendar calendar1 = new GregorianCalendar();
calendar1.set(Calendar.HOUR_OF_DAY, 11);
calendar1.set(Calendar.MINUTE, 00);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.MILLISECOND, 0);
long triggerMillis = calendar1.getTimeInMillis();
alarm2.setRepeating(AlarmManager.RTC_WAKEUP, triggerMillis,
AlarmManager.INTERVAL_DAY, pintent2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.