我想在特定日期通知通知.
然后我现在正在使用带有NotificationManager的AmarmManager.
当我从dateDialog设置所选日期时,警报正在运行.
如何在固定时间内将日历值设置在闹钟设置上?
我希望每天在固定时间重复闹钟,例如早上9点.
目前,警报忽略了特定日期的时间.
你可以帮帮我吗?非常感谢.
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//set alarm with expiration date
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
public void setOneTimeAlarm() {
c.set(Calendar.HOUR_OF_DAY, 10);
c.set(Calendar.MINUTE, 0);
c.set(expiredYear, expiredMonth, expiredDay);
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this,
0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
});
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
expiredYear = year;
expiredMonth = monthOfYear;
expiredDay = dayOfMonth;
displayDate();
}
};
public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Check your fridge";
CharSequence message = "It's time to eat!";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Keep Fridge", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
}
解决方法:
您应该调用public void setRepeating(int type,long triggerAtTime,long interval,PendingIntent operation)(see here)来重复警报.例如,您想要在每天上午9:00触发警报,您可以执行以下操作:
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
此外,在初始化PendingIntent时将最后一个参数设置为0.
PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this,
0, myIntent, 0);
标签:android,calendar,alarmmanager,notificationmanager
来源: https://codeday.me/bug/20190714/1462198.html