AlarmManager RepeatingAlarm 不会多次触发

AlarmManager RepeatingAlarm not firing more than once

好的,我有一个后台刷新,用户可以在设置中切换。这是设置 activity:

中的代码
intent  = new Intent(context, UpdateScoresService.class);
recurringDownloadDaily = PendingIntent.getService(context,0,intent, 0);
recurringDownloadWeekly = PendingIntent.getService(context, 1, intent, 0);
 Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if(newValue.toString().equals("1")){ /* daily */
                        background_refresh.setSummary("Scores will be refreshed daily.");

                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownloadDaily);
                        manager.cancel(recurringDownloadWeekly);
                        Log.e("DAILY REFRESH", " ");
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM DAILY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownloadDaily);
                    }else if(newValue.toString().equals("2")){ /* weekly */
                        Log.e("WEEKLY REFRESH", " ");
                        background_refresh.setSummary("Scores will be refreshed weekly.");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownloadDaily);
                        manager.cancel(recurringDownloadWeekly);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY,10);
                        calendar.set(Calendar.MINUTE,00);
                        if(calendar.before(Calendar.getInstance())){
                            Log.e("AFTER", "10 AM WEEKLY");
                            calendar.add(Calendar.DATE, 1);
                        }
                        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, recurringDownloadWeekly);
                    }else{ /* manually */
                        background_refresh.setSummary("Scores will be refreshed manually.");
                        Log.e("MANUAL REFRESH", " ");
                        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                        manager.cancel(recurringDownloadDaily);
                        manager.cancel(recurringDownloadWeekly);
                    }
                    return true;
                }
            };
            background_refresh.setOnPreferenceChangeListener(refreshListener);

如果用户设置每日闹钟,我希望它在每天早上 10 点刷新。 为了进行测试,当我将日期设置为第二天并将时间设置为 9:59AM 时,闹钟会在上午 10 点左右触发。并且可以手动更改 phone 上的日期和时间,并且每次都会触发。然而,似乎如果我设置选项并简单地等到第二天,警报可能会或可能不会触发。我觉得很多时候,它不会发生。

我也有这个代码知道设备何时完成启动以在设备关闭并重新打开后重新安排警报:

public void onReceive(Context context, Intent intent) {
        Log.e("Boot reciever", "a");
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//            Toast.makeText(context, "Boot Reciever", Toast.LENGTH_SHORT).show();
            Log.e("Boot reciever", "b");
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            intent1  = new Intent(context, UpdateScoresService.class);
            recurringDownload = PendingIntent.getService(context,0,intent1, 0);

            String refreshRate = sharedPreferences.getString("background_refresh", "1");

            if(refreshRate.equals("1")){ /* daily */
                daily(context);
            }else if(refreshRate.equals("2")){ /* weekly */
                weekly(context);
            }else { /* manually */
                manually(context);
            }

        }
    }

    public void daily(Context context){
        Log.e("DAILY REFRESH", "BRUH!");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY,10);
        calendar.set(Calendar.MINUTE,00);
        if(calendar.before(Calendar.getInstance())){
            Log.e("AFTER", "10 AM WEEKLY");
            calendar.add(Calendar.DATE, 1);
        }
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, recurringDownload);
    }
    public void weekly(Context context){
        Log.e("WEEKLY REFRESH", "BRUH!");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY,10);
        calendar.set(Calendar.MINUTE,00);
        if(calendar.before(Calendar.getInstance())){
            Log.e("AFTER", "10 AM WEEKLY");
            calendar.add(Calendar.DATE, 1);
        }
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, recurringDownload);
    }

    public void manually(Context context){
        Log.e("MANUAL REFRESH", "BRUH!");
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.cancel(recurringDownload);
    }

据我所知,如果我在 9:55 处关闭 phone 并重新启动它,这似乎也有效,该服务仍然 运行s。

问题是,不是 运行第二天或几天。我不太确定为什么,也许服务在后台被杀死了?我正在考虑将相同的代码添加到 MainActivity 的 onResume 以重新安排警报,但这似乎有点过分,我应该能够在不这样做的情况下始终如一地达到 运行。

不确定,但请检查以下几点:

  1. 创建了两个 ID 分别为 0 和 1 的唯一警报,但在启动时已完成,仅重新启动了一个 ID 为 0 的警报。
  2. 当使用日期来回更改的重复警报进行测试时,可能会出现问题。由于那天的闹钟可能会被触发,稍后您会更改到以前的日期,因此在重新启动之前不会再次触发那天的闹钟。
  3. 启动完成或分配任何警报后,您必须在分配给该未决意图之前取消之前的警报。
  4. 你的服务没有被杀死,因为它们还没有启动,因为挂起的意图将被触发服务。

如果您仍然遇到问题,请检查此 blog。 (不知道哪里错了,所以贴出我的博客URL 以供参考)