【问题标题】:AlarmManager executing task too oftenAlarmManager 执行任务过于频繁
【发布时间】:2018-02-05 12:17:41
【问题描述】:

该功能的目标是执行一项任务,该任务将应用程序数据库的所有条目中的值设置为 0。 该任务应在每天午夜执行(或当天第一次电话被唤醒(如果我正确理解AlarmManager.RTC))。 问题是任务每天执行几次而不是只执行一次。

在 MainActivity 的 onCreate 中:

// Calendar with timestamp midnight
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

// initiate Alarm to reset commits @ midnight
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);

包含被执行代码的类:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        ContentValues APValues = new ContentValues();
        APValues.put(APEntry.AP_DAILY_COMMIT, 0);

        int rowsUpdated = context.getContentResolver().update(APEntry.CONTENT_URI, APValues, null, null);
        if (rowsUpdated == 0) {
            Toast.makeText(context, "Resetting commits failed!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "Reset commits successful!", Toast.LENGTH_SHORT).show();
        }
    }
}

【问题讨论】:

  • 请告诉我们提供的答案是否能解决您的问题。
  • 已经添加了。明天告诉你。
  • 到目前为止似乎有效。接下来的几天会继续关注它。
  • 很高兴知道。如果我的回答有帮助,您可以点赞或接受。

标签: android task alarmmanager repeat


【解决方案1】:

我不确定为什么您的闹钟会被多次调用。我可以假设您可能多次设置警报。您可以使用SharedPreferences 来检查是否经过了 1 天。因此,您的代码可能如下所示:

public class AlarmReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (dayElapsed(context))
        {
            ContentValues APValues = new ContentValues();
            APValues.put(APEntry.AP_DAILY_COMMIT, 0);

            int rowsUpdated = context.getContentResolver().update(APEntry.CONTENT_URI, APValues, null, null);
            if (rowsUpdated == 0)
            {
                Toast.makeText(context, "Resetting commits failed!", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(context, "Reset commits successful!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean dayElapsed(Context context)
    {
        String lastUpdateTimeKey = "dayElapsed";
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        long lastUpdateTime = sharedPrefs.getLong(lastUpdateTimeKey, -1);
        long currentMillis = System.currentTimeMillis();
        long oneDayMillis = TimeUnit.DAYS.toMillis(1);
        long timeElapsed = currentMillis - lastUpdateTime;

        if (lastUpdateTime < 0 || timeElapsed >= oneDayMillis)
        {
            SharedPreferences.Editor editor = sharedPrefs.edit();
            editor.putLong(lastUpdateTimeKey, currentMillis);
            editor.commit();

            return true;
        }

        return false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 2017-08-03
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    相关资源
    最近更新 更多