如何从上午8点重复报警时至10时的android [英] How to repeat alarm from 8 AM to 10 PM android

查看:178
本文介绍了如何从上午8点重复报警时至10时的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个应用程序在通知来每隔一小时后,从上午8时至晚上10时
我能怎么做
这里是每隔一小时后,code对重复报警

  AlarmManager AM =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    意向意图=新意图(背景下,AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME,Boolean.FALSE);
    的PendingIntent圆周率= PendingIntent.getBroadcast(上下文,0,意图,0);
    //后5秒后
    am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的(),1000 * 60 * 60,PI);


解决方案

的一种方法是只设置报警火每小时一次和比较后,对你的期望的开始和结束时间的唤醒时间。

另一种方法是做同样的,但在晚上10点停止每小时报警,而是设定一个早上8点。再回到每小时那些到晚上10点一次。车床,漂洗,重复。

编辑:

您可以使用这样的事情做对的时候比较:

 进口android.text.format.Time;...私有静态最终诠释START_HOUR = 10;
私有静态最终诠释END_HOUR = 22;...现在时间=新时代();
now.setToNow();INT小时= now.hour;
如果(小时> =&START_HOUR功放;&安培;小时< = END_HOUR)
    // 做一点事;
其他
    //做别的事情;

I want to make an app in which notification come after every one hour from 8Am to 10Pm how can i do here is code for repeat alarm after every one hour

AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    //After after 5 seconds
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 60  , pi);

解决方案

One way would be to just set the alarm to fire once every hour and compare the time upon wake-up against your desired start and end times.

Another way would be to do the same, but discontinue the hourly alarms at 10pm and instead set one for 8AM. Then back to the hourly ones until 10pm again. Lather, rinse, repeat.

EDIT:

You can do the time comparisons using something like this:

import android.text.format.Time;

...

private static final int START_HOUR = 10;
private static final int END_HOUR = 22;

...

Time now = new Time();
now.setToNow();

int hour = now.hour;
if (hour >= START_HOUR && hour <= END_HOUR)
    // do something;
else
    // do something else;

这篇关于如何从上午8点重复报警时至10时的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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