到下一个警报的时间(以秒或毫秒为单位)

发布于 2024-12-15 17:35:58 字数 243 浏览 7 评论 0原文

使用此代码,我可以获取一些字符串,例如“Wed 10:10”,

String nextAlarm = Settings.System.getString(getContentResolver(),
    Settings.System.NEXT_ALARM_FORMATTED);

但我希望获得距离下一个警报的秒数值。这可能吗? 另外,如果我能够读取描述字段的值以及启用此警报的日期,那对我来说会很好。

With this code I am able to take some string like "Wed 10:10",

String nextAlarm = Settings.System.getString(getContentResolver(),
    Settings.System.NEXT_ALARM_FORMATTED);

but I want to have a value of seconds to the next alarm. Is that possible ?
Also it would be nice for me if I am able to read the value of the description field, and the days when this alarm is enabled.

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

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

发布评论

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

评论(1

秋凉 2024-12-22 17:35:58

您需要通过以下方式来完成:

private void updateAlarmDate(String nextAlarm) {
    if (nextAlarm.length()==0 || nextAlarm == "") {
        return;
    }
    else
    {
        DateFormat sdf = new SimpleDateFormat("EEE hh:mm aa");
        Date alarmDate = null;
        try {
            alarmDate = sdf.parse(nextAlarm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int alarmDay = alarmDate.getDay();
        Calendar now = Calendar.getInstance();
        Date currentDate = now.getTime();
        int todayDay = currentDate.getDay();
        int daysDiff = 0;
        if (todayDay > alarmDay) {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7) + 1;
        } else {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7);
        }
        now.add(Calendar.DATE, daysDiff);
        AlarmFulldate = now.getTime();
        AlarmFulldate.setHours(alarmDate.getHours());
        AlarmFulldate.setMinutes(alarmDate.getMinutes());
        AlarmFulldate.setSeconds(0);
    }
}    

long millis = AlarmFulldate.getTime() - new Date().getTime();
变量 millis 将具有毫秒。

使用以下代码从 millis 获取天数、小时数和分钟数

days = (int) (millis / (1000 * 60 * 60 * 24));
hours = (int) ((millis - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
min = (int) (millis - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/(1000 * 60);

希望这对您有帮助

You need to do it by following way

private void updateAlarmDate(String nextAlarm) {
    if (nextAlarm.length()==0 || nextAlarm == "") {
        return;
    }
    else
    {
        DateFormat sdf = new SimpleDateFormat("EEE hh:mm aa");
        Date alarmDate = null;
        try {
            alarmDate = sdf.parse(nextAlarm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int alarmDay = alarmDate.getDay();
        Calendar now = Calendar.getInstance();
        Date currentDate = now.getTime();
        int todayDay = currentDate.getDay();
        int daysDiff = 0;
        if (todayDay > alarmDay) {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7) + 1;
        } else {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7);
        }
        now.add(Calendar.DATE, daysDiff);
        AlarmFulldate = now.getTime();
        AlarmFulldate.setHours(alarmDate.getHours());
        AlarmFulldate.setMinutes(alarmDate.getMinutes());
        AlarmFulldate.setSeconds(0);
    }
}    

long millis = AlarmFulldate.getTime() - new Date().getTime();
Variable millis will have milliseconds.

Use following code to get days, hours and min from millis

days = (int) (millis / (1000 * 60 * 60 * 24));
hours = (int) ((millis - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
min = (int) (millis - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/(1000 * 60);

Hope this will help you

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