How to execute a method in a certain time?

4

I need at certain scheduled times, to execute a method even if the user is outside the application.Why do I research I will have to use services? I did not quite understand how to program the time, I just left the class created here:

public class Notificacao extends Service {


public void onCreate() {

    super.onCreate();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

    //fez_algo

    Toast.makeText(Notificacao.this, "Fez algo", 1000).show();
    return START_STICKY;

}

But how to execute a method, every 1 hour for example? this method can be declared in the same service class, does anyone tell me the simplest form?

    
asked by anonymous 23.07.2014 / 07:23

1 answer

6

Use BroadcastReceiver together with AlarmManager . In the onReceive of BroadcastReceiver method, have the scheduled task run.

public class ExecutarTarefaProgramadaReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //      Código a executar
    }

}  

To start the process use this code:

//Definir a hora de início
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(context,1234, tarefaIntent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer todos os dias às 10 horas
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 AlarmManager.INTERVAL_DAY, tarefaPendingIntent);  

Do not forget to register BroadcastReceiver in AndroidManifest.xml

<receiver android:name="a_sua_package.ExecutarTarefaProgramadaReceiver"/>  

See the AlarmManager documentation for how to set other time slots.

To run 4x per day (every 6 hours), starting at 10 o'clock:

//Definir início para as 10 horas
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

//Definir intervalo de 6 horas
long intervalo = 6*60*60*1000; //6 horas em milissegundos

Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(context,1234, tarefaIntent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer de 6 em 6 horas a partir das 10 horas
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 intervalo, tarefaPendingIntent);

If the task to be run is time-consuming, instead of a Boadccast receiver, use a service to run it. This response shows a possible implementation. The code to be executed should be placed in the onHandleIntent() method. The implementation handles the status of the device being turned off, rescanning the task again when it is turned on.

    
23.07.2014 / 12:59