AlarmManager未在第二天触发警报

时间:2015-01-02 12:10:24

标签: android alarmmanager

我有这个闹钟管理器,我想每天早上6点开火。当应用程序首次启动时,我会调用类通知,因此,它会在每天早上6点(重复)设置重复警报。

当用户从早上6点到9点安装(并首次运行应用程序)时,警报管理器工作正常,但是如果用户在上午9点后第一次运行应用程序,我应该添加一天和第二天凌晨6点发出警报。在这样做时,(将日期改为第二天),警报会在晚上10点左右随机发射(这是一个巨大的差异)。这是我的代码“

    public notification(Context context) {

    this.context=context;

    Intent appstart=new Intent(context,appstartreceiver.class);
    PendingIntent  pi=PendingIntent.getBroadcast(context,0,appstart,PendingIntent.FLAG_UPDATE_CURRENT); // For appstart in the morning
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);

    Calendar currentCal=Calendar.getInstance();
    Calendar startapp = Calendar.getInstance();// FOr app start in the morning

    startapp.set(Calendar.HOUR_OF_DAY,6);
    startapp.set(Calendar.MINUTE, 0);
    startapp.set(Calendar.SECOND, 0);


    long currentTime = currentCal.getTimeInMillis();


    int diff=(int)(currentTime-startapp.getTimeInMillis())/(1000*60*60);

    if (diff<3)
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,startapp.getTimeInMillis(),1000*24*60*60,pi);
    else {

        startapp.add(Calendar.HOUR_OF_DAY, 24);


        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startapp.getTimeInMillis(), 1000 * 24 * 60 * 60, pi);
    }

}

3 个答案:

答案 0 :(得分:2)

用以下代码替换您的代码...这将在每天早上6点开火。它可能对你有帮助。

public void notification(Context context) {

    Intent intent = new Intent(this, appstartreceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 6);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    long startUpTime = calendar.getTimeInMillis();

    // To avoid firing the alarm if the time is passed while setting
    if (System.currentTimeMillis() > startUpTime) {
        startUpTime = startUpTime + 24 * 60 * 60 * 1000;
    }

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);
}

答案 1 :(得分:1)

您可以尝试添加一天

if (calendar.before(Calendar.getInstance())) { calendar.add(Calendar.DAY_OF_MONTH, 1); } ....// your code

这样它会在设定的时间每天开火。

答案 2 :(得分:0)

尝试使用普通软件的清醒意图服务,它可能有所帮助。

public class DailyListener implements AlarmListener {
    public void scheduleAlarms(AlarmManager mgr, PendingIntent pi, Context context) {

            // every day at scheduled time 
            Calendar calendar = Calendar.getInstance();
           // if it's after or equal 6 am schedule for next day
           if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 6) {
                calendar.add(Calendar.DAY_OF_YEAR, 1); 
            }
            calendar.set(Calendar.HOUR_OF_DAY, 6);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

      mgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);


    }
}