AlarmManager RepeatingAlarm不会多次触发

时间:2016-11-01 17:19:57

标签: java android repeatingalarm

好的,我有一个后台刷新,用户可以在设置中切换。以下是“设置”活动中的代码:

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:59时,警报将在上午10点左右触发。并且可以手动更改手机上的日期和时间,并且每次都会触发。但是,似乎我设置了选项并且只是等到第二天,警报可能会或可能不会触发。我觉得这种情况往往不会发生。

我也有这段代码知道设备何时完成启动以在设备关闭并重新启动后重新安排闹钟:

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关闭手机并将其重新启动,这似乎也能正常工作,该服务仍在运行。

问题是,它没有在第二天或第几天运行。我不确定为什么,也许服务在后台被杀?我正在考虑将相同的代码添加到MainActivity的onResume来重新安排警报,但这似乎有点过分,我应该能够让它一直运行而不会这样做。

1 个答案:

答案 0 :(得分:0)

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

  1. 创建了两个独特的闹钟0& 1个ID但在启动完成后,只重启一个0 id的警报。
  2. 使用日期变换的重复警报进行测试时,可能会出现问题。由于当天的警报可能会被触发,之后您又更改为上一个日期,因此在重新启动之前它不会再次触发。
  3. 在启动完成或分配任何警报时,您必须在分配之前取消此待处理意图的先前警报。
  4. 您的服务未被杀死,因为它们尚未启动,因为要触发服务的待处理意图。
  5. 如果您仍然遇到问题,请查看此blog。 (不知道哪里出错了,所以发布我的博客URL以供参考)