AlarmManager设置特定时间执行

时间:2014-11-26 08:32:35

标签: java android date time alarmmanager

我在尝试设置执行AlarmManager的特定时间时遇到了一些问题。这是我安排警报的代码:

public static void scheduleAlarms(Context context) {
    Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

    AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent notificationIntent = new Intent(context, Alarm.class);
    notificationCount = notificationCount + 1;

    notificationIntent.putExtra("NotifyCount", notificationCount);
    PendingIntent pi = PendingIntent.getBroadcast(context, 1,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

我正在尝试将警报管理器设置为在0030运行,如果它符合我的if语句中的条件。但是,尽管某些记录符合要求,但alarmManager不会发送推送通知。所以我改变了代码,每2分钟重复一次:

mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.getTimeInMillis(),
            2*60*60, pi);

它确实发送了推送通知。我想知道当我试图指定一个时间执行时,我为这个部分犯了什么错误。

提前致谢。

0 个答案:

没有答案