startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, startSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
startSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
endSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_END_SLEEP);
endSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, endSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
endSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
checkWifiIntent = new Intent(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiPendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, checkWifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
checkWifiAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
checkWifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (settings.getEnableDisableApp()) {
// TODO: Check is this a good way to handle threads
Thread t = new Thread() {
@Override
public void run() {
//Do Some work
this.interrupt();
// throw new ThreadDeath();
}
};
t.start();
//}
}
}
};
checkWifiIntetntFilter = new IntentFilter(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiIntetntFilter.addAction(CommunicationStrings.ALARAM_END_SLEEP);
checkWifiIntetntFilter.addAction(CommunicationStrings.ALARAM_START_SLEEP);
registerReceiver(checkWifiReceiver, checkWifiIntetntFilter);
private void changeCheckWifiTimerPeriod(long period) {
checkWifiAlarmManager.cancel(checkWifiPendingIntent);
checkWifiAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), period, checkWifiPendingIntent);
}
private void changeStartSleepTimerTime(Calendar alarmTime) {
startSleepTimeAlarmManager.cancel(startSleepTimePendingIntent);
startSleepTimeAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY/*24 * 60 * 60 * 1000*/, startSleepTimePendingIntent);
}
private void changeEndSleepTimerTime(Calendar alarmTime) {
endSleepTimeAlarmManager.cancel(endSleepTimePendingIntent);
endSleepTimeAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, endSleepTimePendingIntent);
}
The wifi check works but the start and end sleep doesn't work....I looked all the posts about the alarm manager and tried it all but I can't figure out why it doesn't work...
what could be the reason?
EDIT: I noticed that it works only the first time I run the app..
I found a solution, I gave unique requestCode for each alarm
startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, startSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
startSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
endSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_END_SLEEP);
endSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 1, endSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
endSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
checkWifiIntent = new Intent(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiPendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 2, checkWifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
checkWifiAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);