毫秒级时间报警 [英] Time in Milliseconds Alarm

查看:212
本文介绍了毫秒级时间报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我的工作有一个报警,让在一个通知,让我们每天说下午3点,但我想这个用户能够进行选择,AM / PM之间,并且时间/最小自由变化。我可能会使用 TimePicker ,这是我的code我到目前为止有:

 公共无效startAlarm(){
            意向意图=新意图(currentDay.this,AlarmReceiver.class);
            发件人的PendingIntent = PendingIntent.getBroadcast(currentDay.this,0,意向,0);            长firstTime = SystemClock.elapsedRealtime();
            firstTime + = 15 * 1000;
            AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,AlarmManager.INTERVAL_DAY,发件人);
        }

所以,我想我将要使用的线沿线的东西:

 日历CAL = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,19);
cal.set(Calendar.MINUTE,45);

,然后用

  cal.getTimeInMillis()

但是,这并不工作,任何想法?谢谢!

编辑:那么,长话短说,我知道如何获取当前时间,再加入可以说15秒的时间,但我想有一个适用于例如下午5:14在一定时间,一切我已经试过不行


解决方案

据我可以告诉你得到一个日历实例与当前的日期/时间,然后要添加19小时45分钟吧,不设明确日历实例的时间19:45。这是你的意思吗?您需要使用日历set()方法设置明确的时间。

日历的API参考


  

Calendar的getInstance方法返回一个日历,其区域设置是基于系统的设置,其的时间字段已经与当前日期和时间初始化

 日历了RightNow = Calendar.getInstance()


 公共抽象无效添加(INT领域,int值)自:API级别1
将指定金额日历场。
参数
    字段中的日历字段进行修改。
    价值要添加到领域的量。
    抛出IllegalArgumentException - 如果字段是DST_OFFSET或ZONE_OFFSET。

编辑::要本地时间转换为UTC ...

 日历CAL = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,19);
cal.set(Calendar.MINUTE,45);
的int偏移= cal.getTimeZone()的getOffset(cal.getTimeInMillis())。
firstTime = cal.getTimeInMillis()+偏移量;

请注意:我还没有试过以上并有可能成为一个更简单的方式,但它应该工作。这是我很难测试这样的东西作为我的时区为GMT / UTC。

Okay, So I'm working on having an alarm that gives a notification at, lets say 3:00 PM daily, but I want this to be selectable by the user, between AM/PM, and Hours/Min freely changeable. I will probably use a TimePicker, and this is my code I have so far:

    public void startAlarm() {


            Intent intent = new Intent(currentDay.this, AlarmReceiver.class);
            PendingIntent sender = PendingIntent.getBroadcast(currentDay.this,0, intent,0);

            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 15*1000;


            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,AlarmManager.INTERVAL_DAY, sender);


        }

So, I figure I'm going to be using something along the lines of:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 45);

and then using

cal.getTimeInMillis()

But this doesn't work, any ideas? Thanks!

EDIT: So, long story short, I know how to get the current time, then add lets say 15 seconds to it, but I want to have a definite time that WORKS for example 5:14 PM, and everything I've tried doesn't work

解决方案

As far as I can tell you are getting a Calendar instance with the current date/time and then you are adding 19 hours and 45 minutes to it, NOT setting the time of the Calendar instance explicitly to 19:45. Is that what you are meaning to do? You need to use the Calendar set() method to set an explicit time.

From the API reference for Calendar

Calendar's getInstance method returns a calendar whose locale is based on system settings and whose time fields have been initialized with the current date and time:

    Calendar rightNow = Calendar.getInstance()

public abstract void add (int field, int value)

Since: API Level 1
Adds the specified amount to a Calendar field.
Parameters
    field   the Calendar field to modify.
    value   the amount to add to the field.
    Throws IllegalArgumentException if field is DST_OFFSET or ZONE_OFFSET.

EDIT: To convert local time to UTC...

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 45);
int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis());
firstTime = cal.getTimeInMillis() + offset;

NOTE: I haven't tried the above and there may be an easier way but it should work. It's hard for me to test stuff like this as my timezone is GMT/UTC.

这篇关于毫秒级时间报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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