通知和AlarmManager

编程入门 行业动态 更新时间:2024-10-09 09:18:40
本文介绍了通知和AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这个问题已经被问了几次,但我真的不明白,为什么我的code不起作用。我基于大量的实例(从计算器解答)写我的code和我需要帮助明白问题出在哪里。

I know that this question has been asked several times but I really don't understand why my code doesn't work. I based upon a lot of examples (from stackoverflow answers) to write my code and I need help to understand where is the problem.

上下文

用户可以,槽式preferences,选个日子要通知。我得到一个数字从2日(星期一),3 ... 4 ... 5 ... 1(星期日)。

The user can, trough preferences, choose a day to be notify. I get a number from 2 (monday) 3...4...5... to 1 (sunday).

public Calendar getCalendar(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK,getDayForNotification()); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.AM_PM,Calendar.AM); return calendar; }

然后我想重复每周一次报警上用户选择的一天。

Then I want to repeat the alarm once a week on the day that the user has chosen.

public void setNotificationDate() { alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, myAlarmReceiver.class); alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); DateNotificationPreference dateNotificationPreference = new DateNotificationPreference(this); alarmMgr.setRepeating(AlarmManager.RTC, dateNotificationPreference.getCalendar().getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, alarmIntent); }

我的广播接收机

public class myAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service1 = new Intent(context, AlarmService.class); context.startService(service1); } }

在我的服务,我有中庸之道一个方法:

In my service, I have juste one method :

public void createNotification(){ final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); final Intent launchNotifiactionIntent = new Intent(this, mySilhouette.class); final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, launchNotifiactionIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.icon_notification) .setContentTitle("my notification") .setContentText("my first notification") .setContentIntent(pendingIntent); mNotification.notify(0, builder.build()); }

当我选择当天,通知出现在好时机,但是当我重新启动应用程序在上午8:05例如,我再有一个通知。在此之后,如果我选择另一天,然后返回到活动中,我又通知...有什么奇怪的,我不知道有什么可以是问题。我需要你的帮助:)

When I choose the current day, the notification appears at the good time, but when I relaunch the app at 8:05 am for example, I have again a notification. After that, if I choose another day and go back to the activity, I have again a notification...There is something strange and I don't know what can be the problem. I need your help :)

非常感谢!

Kmel

推荐答案

设置()在日历不只是即:它设置的值。其结果是,日期可能是在过去,由于现有的code。

set() on Calendar does just that: it sets the value. As a result, the date may be in the past, given your existing code.

例如,我在哪里,现在它是星期五。因此,通过返回Calendar.getInstance()对象现在设置的,这意味着它的星期几,将是周五。如果你调用设置()更改至周二,现在你可以设定在过去的某个日期。同样,即使你是某一天的周设置到周五,它是早晨8点我在哪里后,因此,你会被创建在过去的某个日期。

For example, where I am, right now it is Friday. So, the object returned by Calendar.getInstance() is set for now, which means its day-of-week will be Friday. If you call set() to change that to Tuesday, now you may be setting a date in the past. Similarly, even if you were to set the day-of-week to Friday, it is after 8am where I am, and therefore you would be creating a date that is in the past.

您需要检查你的计算日历对象,看看它是否是在过去。如果是,增加7天,其移动到的下次的一周的时间用于报警熄灭。

You need to check your calculated Calendar object and see if it is in the past. If it is, add 7 days to move it to the next week's time for the alarm to go off.

此外,注意, RTC 闹钟不会唤醒设备退出睡眠模式。这可能是罚款的目的,但我只是想确保你知道了这个情况。

Also, note that an RTC alarm will not wake up the device out of sleep mode. That may be fine for your purposes, but I just wanted to make sure that you were aware of this.

更多推荐

通知和AlarmManager

本文发布于:2023-11-27 11:05:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!