繁体   English   中英

重复通知不重复

[英]Repeating notifications not repeating

我想从设置之日起每周进行一次通知。 它会在被调用时进行初始化,但不会在第二次调用时进行初始化。(我快速转发了电话时钟,以查看是否可以调用它,但没有调用)。 它必须是7 * calendar.getTimeInMillis()。 我还要如何安排每周一次?

        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类:

    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);

7*calendar.getTimeInMillis()确实是个问题,因为calendar.getTimeInMillis()返回自1970年以来的时间,因此您基本上将重复次数设置为从现在开始的〜42.5 * 7年。 您需要设置偏移量,例如7(天)* 24(小时)* 60(分钟)* 60(秒)* 1000(毫米)

清除这些信息后-我建议您避免使用重复,而是在每次被调用的代码完成其工作时都设置一个新警报,因为重复机制可能存在一些问题。

您不需要当前日期* 7:

7天= 60480万毫秒

这是7天内的毫秒数

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

暂无
暂无

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

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