Malfunction alarm

1

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.

    
asked by anonymous 06.08.2014 / 18:23

1 answer

2

I imagine you have already read the official documentation, but anyway I'll leave the guide link as a reference: Scheduling Repeating Alarms

So, the first comment I make, instead of using Random to vary the minutes and seconds, use the setInexactRepeating () (as opposed to setRepeating), so you allow Android to group nearby alarms and save battery power.

Correct is to use a BroadcastReceiver as the basis for the AlarmManager. If you are using a receiver and then firing some Service (Context.startService ()) to perform the task, it is likely that the device is sleeping before completing the task. So, as you say in the official documentation, you will need a new separate wake lock rule to prevent this from happening.

This should not have happened at other times, since you might have the device connected to your computer or something was waiting to happen, so the device would not go into sleep mode.

To do this, add the permission in your Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

And on your receiver, before calling the service do so:

WakefulIntentService.acquireStaticLock(context);
context.startService(new Intent(context, SuaClasseServiceQueExecutaTarefa.class));

Then in Service, be sure to treat the wake lock. I did not find an example in Portuguese, there is a very complete English post: Using IntentService With AlarmManager to Schedule Alarms

Also, when you do:

  

Calendar calendar = Calendar.getInstance ();

The correct one is in the following sequence add:

calendar.setTimeInMillis(System.currentTimeMillis());
    
07.08.2014 / 22:51