每天特定时间的通知 [英] Everyday notifications at certain time

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

问题描述

我想实现这一目标:

首次打开应用程序后,用户会在每天下午 2 点收到通知,如果特定条件为真.如果条件为假,我们今天不会显示通知.下午2点检查条件,从网上下载一些数据.

到目前为止,我使用了 AlarmManager 及其方法 setRepeating()24 小时间隔.AlarmManager 启动一个 Service.在此服务中,我正在下载数据、检查条件以及是否为真 - 显示 Notification.由于下载可以持续 5 秒以上,我已经为这个服务声明了 android:process=":background",在单独的进程中运行它而不阻塞我的 UI.

这种方法有两个缺点:

<小时>

1:如果用户在下午 4 点打开应用程序(并且条件为真),他将立即收到通知.来自 setRepeating() 文档:

<块引用>

如果时间发生在过去,则会触发警报立即,警报计数取决于过去多长时间触发时间与重复间隔有关.

我希望该用户今天不会收到通知,只会在第二天收到通知,以此类推.

<小时>

2:我担心用户关闭手机后我的通知不会显示.来自 AlarmManager 文档:

<块引用>

已注册的闹钟在设备处于睡眠状态时会保留(如果设备在此期间关闭,可以选择将其唤醒),但如果关闭并重新启动,则会被清除.

我不知道是否可以让它一直工作.

<小时>

如果您有任何改进方法,欢迎提出.

解决方案

1: 我不太确定我是否理解你的问题,但我认为你所要做的就是如果已经过了下午 2 点添加一天到下午 2 点:

GregorianCalendar twopm = new GregorianCalendar();twopm.set(GregorianCalendar.HOUR_OF_DAY, 14);twopm.set(GregorianCalendar.MINUTE, 0);twopm.set(GregorianCalendar.SECOND, 0);twopm.set(GregorianCalendar.MILLISECOND, 0);if(twopm.before(new GregorianCalendar())){twopm.add(GregorianCalendar.DAY_OF_MONTH, 1);}alarmManager.setRepeating(类型,twopm.getTimeInMillis(),1000*60*60*24,意图);

2:你可以注册一个 BroadcastReceiver 来启动并在那里再次启动你的闹钟.看看这个:启动时的Android BroadcastReceiver - 当Activity处于后台时保持运行>

I would like to achieve this:

After first turning on the application, user receives notifications, every day at 2pm, if certain condition is true. If condition is false, we are not showing a notification this day. The condition is checked at 2pm, it downloads some data from the Internet.

So far I used AlarmManager and its method setRepeating() with 24h interval. AlarmManager fires up a Service. In this Service I'm downloading the data, checking condition and if it's true - showing Notification. Since downloading can last more than 5 seconds, I've declared android:process=":background" for this Service, to run it in separate process and not block my UI.

This approach has two drawbacks:


1: If user opens application let's say at 4pm (and the condition is true), he will receive the notification immediately. From setRepeating() documentation:

If the time occurs 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.

I would like that user will not receive a notification this day, only the next day and so on.


2: I'm worried that my notifications will not show after user switch the phone off. From AlarmManager documentation:

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

I don't know if it's possible to make it work all the time.


If you have any ideas how to make it better, you're welcome.

解决方案

1: I'm not quite sure if i understood your question, but I think all you have to do is if it already is past 2pm add a day to 2pm:

GregorianCalendar twopm = new GregorianCalendar();
twopm.set(GregorianCalendar.HOUR_OF_DAY, 14);
twopm.set(GregorianCalendar.MINUTE, 0);
twopm.set(GregorianCalendar.SECOND, 0);
twopm.set(GregorianCalendar.MILLISECOND, 0);
if(twopm.before(new GregorianCalendar())){
    twopm.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
alarmManager.setRepeating(type, twopm.getTimeInMillis(), 1000*60*60*24, intent);

2: You could register a BroadcastReceiver for booting and start your alarm again there. Take a look at this: Android BroadcastReceiver on startup - keep running when Activity is in Background

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

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