即使时间过去也重复发出警报

发布于 2024-12-04 08:08:00 字数 862 浏览 0 评论 0原文

我创建了警报演示。在那个演示中,我重复了一个闹钟。我的演示中有一个问题。即使时间过去了,我的闹钟也会呼叫服务。我设置了 16:08:00 时间并调用了该警报,因此它会在过了该时间后调用我的警报服务。请帮助我停止此条件。

 AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(ctx.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 8);
        calendar.set(Calendar.SECOND, 0);   
        PendingIntent pi = createPendingIntent(ctx);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);

CreatePendingIntent方法

private static PendingIntent createPendingIntent(Context context) 
    {
        Intent myIntent = new Intent(context, MyAlarmService.class);
        return PendingIntent.getService(context,0, myIntent, 0); 

    }

i created alarm demo . In that demo i am repeating an alarm . I have one problem in my demo . My alarm called service even if time passed . I am setting 16:08:00 time and called that alarm so it called my alarm service after passed that time.Please help me to stop this criteria.

 AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(ctx.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 8);
        calendar.set(Calendar.SECOND, 0);   
        PendingIntent pi = createPendingIntent(ctx);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);

CreatePendingIntent Method

private static PendingIntent createPendingIntent(Context context) 
    {
        Intent myIntent = new Intent(context, MyAlarmService.class);
        return PendingIntent.getService(context,0, myIntent, 0); 

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

征棹 2024-12-11 08:08:00

当将闹钟设置为过去的时间时,闹钟会立即弹出。
只需检查当前时间是否大于闹钟时间即可。如果是这样,请在闹钟时间上添加 24 小时并设置闹钟:

long timeToAlarm = calendar.getTimeInMillis();
if (calendar.getTimeInMillis() < System.currentTimeMillis())
{
    timeToAlarm += (24*60*60*1000);
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToAlarm, 24*60*60*1000, pi);

When setting an alarm to past time, the alarm immediately pops up.
Simply check if the current time is bigger than the alarm time. If so, add 24 hours to the alarm time and set the alarm.:

long timeToAlarm = calendar.getTimeInMillis();
if (calendar.getTimeInMillis() < System.currentTimeMillis())
{
    timeToAlarm += (24*60*60*1000);
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToAlarm, 24*60*60*1000, pi);
时光倒影 2024-12-11 08:08:00
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);

如果您使用上面的代码,闹钟会以 24*60*60*1000 间隔时间重复。如果您不想重复闹钟,请使用以下代码,

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);

上面的代码将调用闹钟只有一次。

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);

if you used the above code the alarm repeats at the 24*60*60*1000 interval time.If you don't want to repeat the alarm then use the bellow code

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);

the above code will cal the alarm only onetime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文