简体   繁体   English

重复通知不重复

[英]Repeating notifications not repeating

I'd like to have a notification done weekly from the day it was set. 我想从设置之日起每周进行一次通知。 It initializes when it gets called but not the second time.(I fast forwarded the phone clock to see if it would call it but it didn't). 它会在被调用时进行初始化,但不会在第二次调用时进行初始化。(我快速转发了电话时钟,以查看是否可以调用它,但没有调用)。 It must be the 7*calendar.getTimeInMillis(). 它必须是7 * calendar.getTimeInMillis()。 How else would I go about having it set for weekly? 我还要如何安排每周一次?

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, mHour);
        calendar.set(Calendar.MINUTE, mMinute);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, OnBootReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        //am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*calendar.getTimeInMillis(), pendingIntent);

BroadCastReceiver class: BroadCastReceiver类:

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "from";
CharSequence message = "message";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(icon, tickerText, when);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);

The 7*calendar.getTimeInMillis() is indeed the problem as calendar.getTimeInMillis() returns the time since 1970, so you basically set the repeating to ~42.5 * 7 years from now. 7*calendar.getTimeInMillis()确实是个问题,因为calendar.getTimeInMillis()返回自1970年以来的时间,因此您基本上将重复次数设置为从现在开始的〜42.5 * 7年。 You need to set the offset, eg 7 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (millies) . 您需要设置偏移量,例如7(天)* 24(小时)* 60(分钟)* 60(秒)* 1000(毫米)

After we cleared that - I suggest you avoid using the repeating, and instead set a new alarm each time the invoked code finishes its work, as there are some possible problems with the repeating mechanism. 清除这些信息后-我建议您避免使用重复,而是在每次被调用的代码完成其工作时都设置一个新警报,因为重复机制可能存在一些问题。

You don't want the current date * 7 you want: 您不需要当前日期* 7:

7 days = 604 800 000 milliseconds 7天= 60480万毫秒

This is how many milliseconds are in 7 days 这是7天内的毫秒数

ie

 am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 604800000L, pendingIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM