Good afternoon, I'm having problems with AlarmManager in my project, I scheduled it to do an action from 10 to 11, and from 16 to 17 at intervals of 1 day. So the problem is that the other day it's not alarming, I've done several tests with shorter intervals and they worked, I also did the same test with a 1-day interval by manually setting the date and time of the device, and it worked! But when I try to do it normally, I wait the next day to alarm, but it does not work. I think it's the wrong structure because I used AlarmManager inside a Service class, so it follows my project classes:
public class AlarmService extends Service{
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
alarm();
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
/*
* Metodo para gerenciar os alarmes
*/
public void alarm() {
try{
//Variáveis referente a minuto e segundo, fazendo um random
Random min = new Random();
Random sec = new Random();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, min.nextInt(60));
calendar.set(Calendar.SECOND, sec.nextInt(60));
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.HOUR_OF_DAY, 16);
calendar2.set(Calendar.MINUTE, min.nextInt(60));
calendar2.set(Calendar.SECOND, sec.nextInt(60));
Intent tarefaIntent = new Intent(AlarmService.this, NotificacaoReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(
AlarmService.this, 0, tarefaIntent, 0);
Intent tarefaIntent2 = new Intent(AlarmService.this, NotificacaoReceiver.class);
PendingIntent tarefaPendingIntent2 = PendingIntent.getBroadcast(
AlarmService.this, 1, tarefaIntent2, 0);
AlarmManager alarmManager = (AlarmManager) AlarmService.this
.getSystemService(Context.ALARM_SERVICE);
// Define o alarme com intervalos de 1 dia
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
tarefaPendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
tarefaPendingIntent2);
}
catch(Exception e){
Log.e("FadireAlarme", e.getMessage());
}
}
Remembering that the alarm is called when you start the application, or boot the device.