Android闹钟设置与特定日期
我想设置闹钟并在特定日期通知。 然后我目前正在将 AmarmManager 与 NotificationManager 一起使用。 当我从日期对话框中设置选定的日期时,闹钟正在工作。 如何将日历值设置为固定时间的闹钟? 我想每天在固定时间重复闹钟,例如早上 9:00。 目前,警报忽略特定日期的时间。 你能帮我吗?非常感谢。
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);
}
}
I wan to set alarm with notification at specific date.
Then I am using AmarmManager with NotificationManager currently.
When I set selected date from dateDialog, the alarm is working.
How can I put calendar value on alarm set with fixed time?
I want to repeat alarm every day on fixed time such as at 9:00 in the morning.
Currently, alarm is ignoring the time on specific date.
Could you help me? Many thanks.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想在月份的特定日期播放闹钟,假设您想在 2012 年 6 月 11 日播放闹钟,那么您应该这样指定,但 6 月是 6 个月,那么您应该在 Calender.Month 中指定 5。
If you want to play alarm for particular date of months suppose you want to play alarm on 11-june-2012 then you should specify like this but June is 6 month then you should specify 5 in Calender.Month.
嘿,这是如何在 android AlarmManager 上将闹钟设置为特定日期
(android Alarmmanager 在日期设置闹钟)
我一直在寻找这个。
注意这个月的价值!!
Hey this is how to set alarm on android AlarmManager to spesific date
(android alarmmanager set alarm on date)
I have been searching all over for this.
pay attention to the value of the month!!
您应该调用
public void setRepeating (int type, long triggerAtTime, long Interval, PendingIntent operation)
(参见此处)重复闹钟。例如,您想在每天上午 9:00 触发闹钟,您可以这样做:另外,在初始化 PendingIntent 时将最后一个参数设置为 0。
You should call
public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
(see here) to repeat the alarm. For example, you want to fire the alarm on 9:00 am every day, you can do :Also,set the last parameter to 0 when initilazing the PendingIntent.
这可能对你有帮助
This may help you