在使用AlarmManager每天的特定时间的Andr​​oid每日重复的通知 [英] Android daily repeating notification at specific time of a day using AlarmManager

查看:226
本文介绍了在使用AlarmManager每天的特定时间的Andr​​oid每日重复的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Android应用程序发送通知至上午8点,下午3点和晚上8点,每天提醒用户。所以我用的onCreate在MainActivity以下三行(),应用程序启动时。然而,当我运行应用程序,所有三个通知来了一次,而不是在需要时间。

  setRepeatedNotification(1,8,0,0);
    setRepeatedNotification(2,15,0,0);
    setRepeatedNotification(3,20,0,0);

这是为什么?我也在这里附上setRepeatedNotification功能。谢谢!

 私人无效setRepeatedNotification(INT ID,INT 11H,INT毫米,诠释SS){
    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
    意图alarmIntent =新意图(MainActivity.this,AlarmReceiver.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(MainActivity.this,ID alarmIntent,0);    台历挂历= Calendar.getInstance();
   // calendar.set();
    calendar.set(Calendar.HOUR_OF_DAY,11H);
    calendar.set(Calendar.MINUTE,毫米);
    calendar.set(Calendar.SECOND,SS);    //清除previous日常是否存在未决的意图。
    如果(NULL!= mEverydayPendingIntent){
        alarmManager.cancel(mEverydayPendingIntent);
    }
    mEverydayPendingIntent =的PendingIntent;
    alarmManager.setRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,mEverydayPendingIntent);
}


解决方案

我看到您的设置与AlarmManager两个潜在的问题。第一,当设备进入睡眠状态就出现了。

从AlarmManager的文档:


  

如果一个报警延迟(由系统睡眠,例如,用于非_WAKEUP报警类型),跳过的重复将尽快交付。在此之后,未来的报警将按照原定计划交付;它们不随时间漂移。例如,如果你设置循环闹铃每一个小时的顶部,但手机已经睡着了,从7:45至8:45,警报将尽快手机唤醒发送,则下一个报警将在发送9:00。


正如你所看到的,如果你设置闹钟和设备已经进入了梦乡,而无需使用 AlarmManager.RTC_WAKEUP 有可能取决于如何是一个较长时间的延迟长装置已在睡眠。如果你从来没有碰到你的设备并没有其他的报警引起了唤醒时,它可能会导致所有的报警后,该设备是清醒接下来的一个小时叠起来。


我看到另一个潜在的问题是,要检索日历实例重新$ P $现在psenting的时间,但随后被自己设置的小时,分​​钟和秒。当前日期和当年有从当前时刻被自动填充。

再次从文档(重点煤矿):


  

如果规定的触发时间在过去,报警器就会被触发立即,以取决于触发时间是如何在过去远是相对于重复间隔报警计数。


在此情况下,如果你的方法被调用过去的8日下午在某一天, calendar.getTimeInMillis()将在过去的返回一个时间戳所有三个报警,使他们立即从早上8点,下午3点和晚上8点有已经过去在这一天被触发。在这种情况下,你必须首先评估当前时间是否过去,你要设定加1一天多到要设置以确保报警的未来设定的时间间隔报警

I need to the Android app to send notification to remind users at 8am, 3pm and 8pm every day. So I use the following three lines in onCreate() of the MainActivity, when the application starts. However, when I run the app, all three notification are coming at once instead of at the wanted time.

    setRepeatedNotification(1,8,0,0); 
    setRepeatedNotification(2,15,0,0); 
    setRepeatedNotification(3,20,0,0);    

Why is that? I also attach the setRepeatedNotification function here. Thank you!

private void setRepeatedNotification(int ID, int hh, int mm, int ss) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ID, alarmIntent, 0);

    Calendar calendar = Calendar.getInstance();
   // calendar.set();
    calendar.set(Calendar.HOUR_OF_DAY, hh);
    calendar.set(Calendar.MINUTE, mm);
    calendar.set(Calendar.SECOND, ss);

    // Clear previous everyday pending intent if exists.
    if (null != mEverydayPendingIntent) {
        alarmManager.cancel(mEverydayPendingIntent);
    }
    mEverydayPendingIntent = pendingIntent;
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mEverydayPendingIntent);
}

解决方案

I see two potential problems with your setup with AlarmManager. The first arises when the device goes to sleep.

From AlarmManager's documentation:

If an alarm is delayed (by system sleep, for example, for non _WAKEUP alarm types), a skipped repeat will be delivered as soon as possible. After that, future alarms will be delivered according to the original schedule; they do not drift over time. For example, if you have set a recurring alarm for the top of every hour but the phone was asleep from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, then the next alarm will be sent at 9:00.

As you can see, if you've set an alarm and the device has gone to sleep, without using AlarmManager.RTC_WAKEUP there could be a long delay depending on how long the device has been in sleep for. If you've never touched your device and no other alarms caused a wakeup, it could cause all your alarms to stack up upon the next hour that the device is awake for.


Another potential issue I see is that you are retrieving a Calendar instance representing the time right now, but then setting the hour, minute and second by yourself. The current day and the current year have been automatically populated from the current time.

Again, from the documentation (emphasis mine):

If the stated trigger time is in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.

In this case, if your method was invoked past 8 pm on the given day, calendar.getTimeInMillis() will return a timestamp in the past for all three alarms, causing them to be triggered immediately since 8 am, 3 pm and 8 pm have already past in that day. In this case, you must first evaluate whether the current time is past the alarm interval you are trying to set and add 1 day more onto the time you are setting to make sure the alarm has been set in the future.

这篇关于在使用AlarmManager每天的特定时间的Andr​​oid每日重复的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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