gpt4 book ai didi

android - 以毫秒为单位的时间警报

转载 作者:行者123 更新时间:2023-11-29 22:30:00 24 4
gpt4 key购买 nike

好的,所以我正在努力制作一个警报,可以在每天下午 3:00 发出通知,但我希望用户可以选择它,在上午/下午和小时/分钟之间自由更改.我可能会使用 TimePicker,这是我目前的代码:

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


}

所以,我想我将使用以下内容:

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

然后使用

cal.getTimeInMillis()

但这行不通,有什么想法吗?谢谢!

编辑:所以,长话短说,我知道如何获取当前时间,然后添加 15 秒,但我想要一个确定的时间,例如下午 5:14,以及我的所有内容试过不行

最佳答案

据我所知,您正在获取一个带有当前日期/时间的 Calendar 实例,然后向其添加 19 小时 45 分钟,而不是将 Calendar 实例的时间明确设置为 19:45。那是你的意思吗?您需要使用 Calendar set() 方法来设置明确的时间。

来自 Calendar 的 API 引用

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.

编辑:要将本地时间转换为 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;

注意:我还没有尝试过上面的方法,可能有更简单的方法,但它应该有效。我很难测试这样的东西,因为我的时区是 GMT/UTC。

关于android - 以毫秒为单位的时间警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com