My app is doing Human Activity Recognition and notifying the user if they are not active enough. I am reading the accelerometer's data in an always running foreground service. At the moment I am trying to start and stop the service when the user is sleeping (e.g. the user sets Sleeping Hours interval 8pm - 8am) so the service should be stopped and started back on after the specified time. Currently, the following code does not work and I am not sure why. Any suggestions, please?
Note: I know, I will make the code reuse the Calendar instance somehow, this is just for debugging purposes and get it working first.
Date startSleepingHours = mUser.getStartSleepingHours();
Date stopSleepingHours = mUser.getStopSleepingHours();
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, startSleepingHours.getHours());
cal.set(Calendar.MINUTE, startSleepingHours.getMinutes());
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent startServiceIntent = new Intent(ActiveMinutesActivity.this, IntentBroadcastedReceiver.class);
Intent stopServiceIntent = new Intent(ActiveMinutesActivity.this, IntentBroadcastedReceiver.class);
stopServiceIntent.putExtra("command", "STOP_SERVICE");
PendingIntent stopServicePIntent = PendingIntent.getService(ActiveMinutesActivity.this, 0, stopServiceIntent, 0);
PendingIntent startServicePIntent = PendingIntent.getService(ActiveMinutesActivity.this, 1, startServiceIntent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 1000, stopServicePIntent);
Calendar cal2 = new GregorianCalendar();
cal2.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal2.set(Calendar.HOUR_OF_DAY, stopSleepingHours.getHours());
cal2.set(Calendar.MINUTE, stopSleepingHours.getMinutes());
cal2.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal2.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal2.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal2.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), 60 * 1000, startServicePIntent);
And my Receiver class:
public class IntentBroadcastedReceiver extends BroadcastReceiver {
public IntentBroadcastedReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getExtras() != null){
if(intent.getExtras().get("command").equals("STOP_SERVICE")){
context.stopService(new Intent(context, ActiveMinutesService.class));
}else {
context.startService(new Intent(context, ActiveMinutesService.class));
}
}
}
}