在检测到新的一天时调用方法

时间:2013-11-17 15:38:38

标签: java android sharedpreferences days

我希望我的应用程序检测启动时的新的一天。如果是新的一天,我希望它开始新的活动并重复循环。第二天。

我从日历实例开始,获取一年中的某一天。

Calendar c = Calendar.getInstance();
        int thisDay = c.get(Calendar.DAY_OF_YEAR);
        long todayMillis = c.getTimeInMillis();

然后我将其存储在共享首选项中并获得另一个日期

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        long last = prefs.getLong("date", System.currentTimeMillis());
        c.setTimeInMillis(last);
        int lastDay = c.get(Calendar.DAY_OF_YEAR);

我无法运行创建支票并每天循环播放。

if (lastDay != thisDay) {
        scheduleAlarm();
        SharedPreferences.Editor edit = prefs.edit();
        edit.putLong("date", todayMillis);
        edit.commit();

}

我的解决方案如下。

Calendar c = Calendar.getInstance();
        int thisDay = c.get(Calendar.DAY_OF_YEAR);
        long todayMillis = c.getTimeInMillis();

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        long last = prefs.getLong("date", System.currentTimeMillis());

        c.setTimeInMillis(last);
        int lastDay = c.get(Calendar.DAY_OF_YEAR);
        // Toast.makeText(getApplicationContext(),
        // "lastday " + lastDay + "thisDay " + thisDay, Toast.LENGTH_LONG)
        // .show();
        if (lastDay == thisDay) {
            scheduleAlarm();
            SharedPreferences.Editor edit = prefs.edit();
            edit.putLong("date", todayMillis + 86400000);
            edit.commit();
        }

2 个答案:

答案 0 :(得分:3)

将一些关于Joda-Time的注释添加到Sam's correct answer

Joda-Time 2.3让这项工作更轻松。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// A good practice is to specify your time zone rather than rely on default.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");

// Store the datetime last examined.
org.joda.time.DateTime lastUsedStartOfDay = new org.joda.time.DateTime(californiaTimeZone).withTimeAtStartOfDay();

在计时器......

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// In timer…
org.joda.time.DateTime now = new org.joda.time.DateTime(californiaTimeZone);
// Get the starting moment of today.
// Call "withTimeAtStartOfDay" rather than calculate midnight. Not all days in all time zones have a midnight.
org.joda.time.DateTime startOfToday = now.withTimeAtStartOfDay();
// Joda-Time has methods for comparing before, after, equals.
if ( startOfToday.isAfter(lastUsedStartOfDay)) {
    // New day has dawned.
    lastUsedStartOfDay = startOfToday; // Remember this new day.
    System.out.println( "New day has dawned: " + lastUsedStartOfDay );
} else {
    // Same day still.
    System.out.println( "Same day: " + lastUsedStartOfDay );
}

关于Joda-Time ......

    // Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
    // http://www.joda.org/joda-time/

    // Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
    // JSR 310 was inspired by Joda-Time but is not directly based on it.
    // http://jcp.org/en/jsr/detail?id=310

    // By default, Joda-Time produces strings in the standard ISO 8601 format.
    // https://en.wikipedia.org/wiki/ISO_8601

    // About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

    // Time Zone list: http://joda-time.sourceforge.net/timezones.html

我省略了线程安全等实际问题。


未经请求的提示:您可能希望在新的一天开始之后将活动(sleep your thread)延迟到几秒甚至几分钟。大多数操作系统在午夜时分进行大量工作,例如滚动日志。因此,计算中午夜的行程变为witching hour。最好让这些实用工具和守护进程在完成你的工作之前完成他们的家务。

答案 1 :(得分:2)