特定日期和时间的AlarmManager [英] AlarmManager at specific date and time

查看:159
本文介绍了特定日期和时间的AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定的日期和时间(2012年12月25日,12:00)显示一个对话框,我正在使用此代码。我设置为11个月(因为0是gen),但是警报没有开始。我的错是什么?

I'd like to show a dialog at specific date and time (25/12/2012 at 12.00) and I am using this code. I set like 11 month (because 0 is gen) but the alarm does not start. What is my mistake?

Calendar cal=Calendar.getInstance();
    cal.set(Calendar.MONTH,11);
    cal.set(Calendar.YEAR,2012);
    cal.set(Calendar.DAY_OF_MONTH,25);
    cal.set(Calendar.HOUR_OF_DAY,12);
    cal.set(Calendar.MINUTE,00);
    cal.set(Calendar.SECOND,0);

Intent _myIntent = new Intent(context, Notify.class);
    PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);  


推荐答案

看下面关于我将如何设置的代码12月月份的警报:

Look at below code about how i am going to set the alarm for the December month:

// for Alarm 25/12/2012 at 12.00   
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(2012, 11, 25, 12, 00, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent _myIntent = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
_myIntent.putExtra("MyMessage","HERE I AM PASSING THEPERTICULAR MESSAGE WHICH SHOULD BE SHOW ON RECEIVER OF ALARM");
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);

您可以使用自己的意图和课程更新上面的代码,您将获得期望的输出。

You can update the above code with your intent and class and you will get your desire output.

希望对您有所帮助。

这篇关于特定日期和时间的AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆