【问题标题】:How can i set the alarm on the particular date in android?如何在android中的特定日期设置闹钟?
【发布时间】:2016-09-25 13:31:11
【问题描述】:

说明: 我有日历,我在给定月份的特定日期设置事件。事件存储到数据库中。在事件发生当天触发警报以通知用户。

假设,我的活动保存在 2016 年 5 月 29 日,然后我的闹钟在特定日期触发。

注意:我在特定月份创建了多个事件。例如5月29日或30日也可以。

MyQuestion 是我如何在特定的多天触发多个警报。

请理解我到底想要什么流程?

【问题讨论】:

标签: android


【解决方案1】:

如果你想在特定日期和时间播放闹钟,下面的代码可能会对你有所帮助

Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);

cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);

Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent =      
PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent,   
PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
AlarmManager myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +   (10 * 1000), _myPendingIntent);
myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),  _myPendingIntent); 

【讨论】:

【解决方案2】:
 calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,date.getYear());
        calendar.set(Calendar.MONTH,date.getMonth()+1);
        calendar.set(Calendar.DAY_OF_MONTH,date.getDate());
        calendar.set(Calendar.HOUR_OF_DAY,time.getHours());
        calendar.set(Calendar.MINUTE,time.getMinutes());
        calendar.set(Calendar.SECOND,time.getSeconds());
        Intent myIntent = new Intent(context, AlarmReciever.class);
        myIntent.putExtra("ReminderDetails",reminderDetails);

        myIntent.putExtra("requestCode", requestCode);
        pendingIntent = PendingIntent.getBroadcast(context, (int) (requestCode), myIntent,0);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
                    interval, pendingIntent);

下面的代码非常适合在指定的时间和日期设置日历时间。 这里最重要的是你在 alarmManager.setInexactRepeating(_) 方法的第二个参数中传递了什么,即 date.getTime(),这是唯一可行的方法,而不是使用 calendar.getTimeinMillis() 方法,因为这将返回一些意外的时间来启动意图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多