Alarm with malfunction

Asked

Viewed 235 times

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.

1 answer

2


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

Therefore, the first observation I make, instead of using Random to vary the minutes and seconds, use the method setInexactRepeating() (instead of setRepeating), so you allow Android to group the next alarms and save battery.

The correct is to use a Broadcastreceiver as a basis for 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 asleep before completing the task. So, as it says 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, because you might have the device connected to your computer or waiting for something to happen, and with that the device did not enter Sleep mode.

To do so, add the permission to 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, don’t forget to treat the Wake lock. I did not find an example in Portuguese, has this post in English that is very complete: Using Intentservice With Alarmmanager to Schedule Alarms

Besides, when you do:

Calendar Calendar = Calendar.getInstance();

The correct is right in the sequence add:

calendar.setTimeInMillis(System.currentTimeMillis());
  • I am not ultilizing any broadcastReceiver to call the alarmService, I call straight by activity. That will be the problem?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.