[英]Android Set notification to specific time using service
我正在使用服務進行通知。 但我現在遇到的問題是通知發生在上午11點和下午12點,雖然我希望通知只發生在上午11點。
下面的方法是我如何設置時間。 我想知道當我在不同的課程上設置時間時,我的服務類如何知道設置的時間。 甚至從我的服務類調用此方法。
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);
}
這將在上午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.