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

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

问题描述

我想实现这一点:

在第一次打开应用程序,用户会收到通知,每天下午2点,如果某些条件为真。如果条件为假,我们都没有显示通知这一天。条件是下午2点检查,它可以从网上下载一些数据。

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.

到目前为止,我用 AlarmManager 及其方法setRepeating()用24小时的时间间隔。 AlarmManager激发了服务。在这个服务,我下载数据,检验条件,如果这是真的 - 显示通知。由于下载可以持续超过5秒,我已经声明安卓程序=:背景此服务,在单独的进程中运行它并不能阻止我的用户界面。

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.

该方法有两个缺点:

1:如果用户打开应用程序,让我们下午4时说,(和条件为真),他将收到通知的立即。从<一个href="http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29">setRepeating()文档:

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:我很担心我的通知将不会显示后用户关闭手机。从<一个href="http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29">AlarmManager文档:

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:我不太清楚,如果我理解你的问题,但我认为你需要做的是,如果它已经是过去的下午加一天下午2:

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:您可以注册一个BroadcastReceiver来引导和启动你的闹钟再次出现。看看这个:安卓的BroadcastReceiver在启动时

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

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