I am creating an application that will run notifications at specific time intervals when users wake up.
I have an alarmManager running inside a service. The service is explicitly launched by pressing a button on the main event and has an alarmManager that runs notifications during certain time intervals. How can I stop notifications during certain hours of the day? I do not want this notification to be triggered, for example, while the user is sleeping.
My code, which is currently triggering notifications at user-set intervals, is lower (import removed ... this is already long enough):
public class FartSmackinChunks extends Service { public Notification scheduleNotification; public AlarmManager alarmScheduleManager; public PendingIntent alarmScheduleIntent; private Boolean autoUpdateBoolean = true; private int intervalsGoneByInt = 0; private Notification notification; public static final int NOTIFICATION_ID = 1; @Override public void onCreate() { // TODO: Actions to perform when service is created. int icon = R.drawable.icon; String tickerText = "INTERVAL FIRED"; long when = System.currentTimeMillis(); scheduleNotification = new Notification(icon, tickerText, when); alarmScheduleManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); String ALARM_ACTION; ALARM_ACTION = ScheduleAlarmReceiver.ACTION_REFRESH_SCHEDULE_ALARM; Intent intentToFire = new Intent(ALARM_ACTION); alarmScheduleIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0); } @Override public int onStartCommand(Intent intent, int flags, int startId) { SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); boolean autoUpdateBoolean = mySharedPreferences.getBoolean("storedAutoUpdateBoolean", false); String updateFreq = mySharedPreferences.getString("storedInitialAverageTimeInterval", "00:00:00"); SimpleDateFormat dfInterval = new SimpleDateFormat("hh:mm:ss"); Date intervalTimeAsDateObject = null; long updateFreqMilliLong; try { intervalTimeAsDateObject = dfInterval.parse(updateFreq); } catch (ParseException e) { e.printStackTrace(); } updateFreqMilliLong = intervalTimeAsDateObject.getTime() - 18000000; if (autoUpdateBoolean) { int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timetoRefresh = SystemClock.elapsedRealtime() + updateFreqMilliLong; alarmScheduleManager.setInexactRepeating(alarmType, timetoRefresh, updateFreqMilliLong, alarmScheduleIntent); notifications(); } else alarmScheduleManager.cancel(alarmScheduleIntent); return Service.START_NOT_STICKY; }; private void notifications() { **notification stuff in here*** } @Override public IBinder onBind(Intent intent) { // TODO: Replace with service binding implementation. return null; } @Override public void onDestroy() { this.alarmScheduleManager.cancel(alarmScheduleIntent); } }
..... and the implementation of my broadcast receiver is here:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class ScheduleAlarmReceiver extends BroadcastReceiver { public static final String ACTION_REFRESH_SCHEDULE_ALARM = "com.application.ACTION_REFRESH_SCHEDULE_ALARM"; @Override public void onReceive(Context context, Intent intent) { Intent startIntent = new Intent(context, SmokerReducerService.class); context.startService(startIntent); } }
I had difficulty moving my brain around how this should be implemented.
I thought to rework this code so that alarmManager starts at waketime and stops in sleepTime, and yet there is a timer inside the service that starts the notification method at certain intervals? Is there a better way to do this?
Any input would be appreciated. I have been trying to do this in my head for several days.
thank
EDIT: @anyone, which comes across this, intending to use a timer for daily notifications:
The timer that starts inside the service pauses at runtime when the device goes into standby mode (that is ... the user puts the phone in standby mode). Therefore, using a timer to notify you of errors at certain times will not work correctly in the service, because when Android pauses service, it also pauses the timer, which drops the interval.
The right way to do this is to use the AlarmManager with an array of pending intentions to set alarms at specific times during the day. This ensures that even if the phone is put on standby, notifications (or whatever you want to do at this time) will be executed.