代码之家  ›  专栏  ›  技术社区  ›  3dwa21

AlarmManager执行任务过于频繁

  •  0
  • 3dwa21  · 技术社区  · 6 年前

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

    在创建主活动时:

    // 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();
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Ayaz Alifov    6 年前

    我不知道为什么你的闹钟会被多次呼叫。我可以假设你可能多次设置了警报。您可以使用 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;
        }
    }