Question

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..

Was it helpful?

Solution 2

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);

OTHER TIPS

The code is not actually starting the alarms with the AlarmManager. For example, to get your sleep alarm, you'd need to do something like this:

startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent =
    PendingIntent.getBroadcast(BackgroundService.this,
                               0,
                               startSleepTimeIntent,
                               PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        5000, //  Assuming a 5 second wake up
        startSleepTimePendingIntent);

Also remember that these are 1-shot alarms. So after you receive the pending intent, you'd have to start another one with the AlarmManager in order for it to fire again int he future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top