What is the best way to propose recurring tasks in Android?

1

I want a service to run on the mobile phone permanently and every X time to execute certain actions. For the moment I have it raised with an alarm that launches a PendingIntent

PendingIntent alarma = PendingIntent.getActivity(Inicio.this, i, tarea, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendarioPospuesto.getTimeInMillis(), 1000 * 60 * 1440, alarma); 

The question I have is if there is no more robust and efficient approach.

    
asked by Pedraz 11.10.2016 в 18:30
source

1 answer

1

From the Android design point of view, it is not desirable that there is code running always in the background, because this prevents the CPU from sleeping, notably increasing the use of the battery (because you have to use a wake lock for that the code keeps running).
On the other hand, there is no mechanism that guarantees that a service is running permanently (perhaps using the awful sticky ), since the services are tied to the applications, the user can always kill the service from a task manager.

That is why the approach you are currently using is the most suitable:

  • You do not have code making wake lock of the CPU permanently.
  • While the onReceive() method is running you are guaranteed the wake lock .
  • There will be no service to be closed by the user, so you will have more guarantees that your code will be executed.
  • Keep in mind that you have to re-subscribe the alarms if the device restarts, although this is something that you have to control also with the other implementation.

    More information about wake lock here

        
    answered by 11.10.2016 / 18:46
    source