【问题标题】:Reset variable & trigger notification at specific times every day - even when app is closed每天在特定时间重置变量并触发通知 - 即使应用程序关闭
【发布时间】:2023-12-29 23:04:01
【问题描述】:

到目前为止,我一直像这样使用AlarmManagerBroadcastReceiver

private void setUpStreakResetAlarm() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);

        Intent intent = new Intent(getApplicationContext(), DailyCounterCheckReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }

在我的DailyCounterCheckReceiver 课堂上:

@Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        int counterOld = sharedPreferences.getInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_OLD_KEY, 0);
        int counterToday = sharedPreferences.getInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_KEY, 0);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        // If user has increased counter on this day, increase the old check for tomorrow
        if (counterToday > counterOld) {
            counterOld = counterToday;
            editor.putInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_OLD_KEY, counterOld);
            editor.putBoolean("increasedOld", true);
            editor.putBoolean("reset", false);
        }
        // etc.....

但是使用new Android versions这样的后台任务会被杀死,而且非常不可靠。

那我可以用什么代替呢?工作经理,前台服务,还有别的吗? 而且我还不需要 AlarmManager 来触发它们吗?

我的用例非常简单,所以我认为我不需要一些超级复杂的解决方案,但是有很多选择。我的简单案例有什么更好的选择?


编辑:
Angel 的评论会解决我的重置问题,但我也会在某些时候触发通知:

@Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("lastTimeOfNotifTrigger", System.currentTimeMillis());
        editor.apply();

        repository = new NotificationRepository(context);
        repository.startGetNextNotificationAsync(this);
    }

我怎样才能以最简单的方式解决这个问题?

【问题讨论】:

  • 不需要在午夜重置首选项的后台服务,只需为“上次访问日期”添加另一个首选项。然后 onCreate(),如果此“上次访问日期”超过 24 小时,则重置您的其他首选项并更新“上次访问日期”。
  • @AngelKoh 说得很好,谢谢。但我也这样做以在早上触发提醒通知,所以我想我无论如何都需要使用某种服务。我将编辑我的问题。

标签: java android broadcastreceiver android-notifications background-process


【解决方案1】:

您可以使用 alarmManager.setExact 来安排闹钟(即使手机处于睡眠或打盹模式,您的接收器也会在您想要的时间准确地被呼叫)。呼叫您的接收器后,您可以安排下一个。请记住还要添加检测 BOOT 和 PACKAGE_CHANGED 的接收器,以便在手机重启或应用更新时重新安排警报。

如果您不需要在准确的时间运行它,您也可以使用您所说的工作管理器来安排作业。

如果您不尝试从该接收器启动 Activity(您无法再从后台启动 Activity),那么您的代码在较新版本的 Android 中运行应该不会有任何其他问题。

【讨论】:

  • 但是看看这个this question,开始活动不是问题,人们似乎遇到了各种各样的麻烦。但是现在连续几天使用 Andorid 10 设备在真实设备上对其进行测试,它实际上似乎有效。但它在我的任何模拟器上都不起作用,所以我很担心。我想那是出于其他原因。
  • 但是为什么这么多人抱怨呢?在这种情况下使用 AlarmManager 是否错误,或者为什么 Work Manager 应该更好?
最近更新 更多