AlarmManager 未在正确的时间发送通知

问题描述

我正在创建一个应用程序。在这个应用程序中我需要发送通知。但是我有一个问题。当通知间隔为一分钟时,它运行良好。但是,当我尝试将通知间隔更改为 15 分钟或更长时间时,通知会提前发送。而且只做一次。我搜索并尝试了上述不同的方法(setwindow,setexact)。但是我仍然遇到这个问题。

public void sendNotifications(){
        Calendar calendar = Calendar.getInstance();

        if ((calendar.getTime().getHours() >= starthour) && (calendar.getTime().getHours() <= stopHour )){
            if((startminute<calendar.getTime().getMinutes()) || (calendar.getTime().getHours() >= starthour) && (stopMinute>calendar.getTime().getMinutes() ||calendar.getTime().getHours() <= stopHour) ){



                Intent intent = new Intent(WaterSettingsActivity.this,NotifyReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getbroadcast(WaterSettingsActivity.this,intent,0);

                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                int n = sharedPreferences.getInt("spnNotify",0);

                long notificationsFrequency = Long.parseLong(notifySpinner[n]) *1000*60;



                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),notificationsFrequency,pendingIntent);
                Log.i("w",String.valueOf(calendar.getTimeInMillis()));
                Log.i("w",String.valueOf(notificationsFrequency));
            }

解决方法

试试这个

if (userModel.isReminderOn) {
                //region Enable Daily Notifications
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());

                Logger.e("call_cln_day :: " + userModel.scheduleWeekDay + " call week :: " + userModel.scheduleTime);

                calendar.set(Calendar.DAY_OF_WEEK,userModel.scheduleWeekDay);
                calendar.set(Calendar.HOUR_OF_DAY,userModel.scheduleTime);
                calendar.set(Calendar.MINUTE,AppConstant.MIN);
                calendar.set(Calendar.SECOND,0);
                // if notification time is before selected time,send notification the next day
                if (calendar.before(Calendar.getInstance())) {
                    calendar.add(Calendar.DATE,1);
                }
                if (manager != null) {
                    manager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AppConstant.INTERVAL,pendingIntent);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,pendingIntent);
                    }
                }
                //To enable Boot Receiver class
                pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
            } else {
                //Disable Daily Notifications
                if (PendingIntent.getBroadcast(context,alarmIntent,0) != null && manager != null) {
                    manager.cancel(pendingIntent);
                }
                pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
            }