1
Good afternoon, I am having problems regarding the Alarmmanager in my project, I programmed for him to make an action from 10 to 11, and from 16 to 17 with intervals of 1 day. So the problem is that the other day he is not alarming, I have done several tests with shorter intervals and worked, I also did the same test with an interval of 1 day altarando the date and time of the device manually , and it worked! But when I try normally, I wait for the next day to alarm, but it doesn’t work.I think the structure is wrong because I used Alarmmanager within 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 starting the application, or boot on the device.
I am not ultilizing any broadcastReceiver to call the alarmService, I call straight by activity. That will be the problem?
– War Lock
@Warlock yes, it is necessary to register the receiver. In the link I passed at the beginning of the reply there is a complete example of how to use and register alarms. Take a look, and anything I can help editing my answer.
– Neto Marin