AlarmManager 执行任务过于频繁
AlarmManager executing task too often
该功能的目标是执行一项任务,将应用程序数据库的所有条目中的值设置为 0。
该任务应该在每天午夜执行(或者 phone 在当天第一次被唤醒(如果我理解 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);
包含执行代码的class:
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();
}
}
}
我不明白为什么你的闹钟被叫了多次。我可以假设您可能多次设置闹钟。您可以使用 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;
}
}
该功能的目标是执行一项任务,将应用程序数据库的所有条目中的值设置为 0。
该任务应该在每天午夜执行(或者 phone 在当天第一次被唤醒(如果我理解 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);
包含执行代码的class:
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();
}
}
}
我不明白为什么你的闹钟被叫了多次。我可以假设您可能多次设置闹钟。您可以使用 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;
}
}