Start a service at a specific time

0

I am trying to create a service that runs and executes some tasks in back. It has to start running at one hour and finish at another.
For example, a service that starts running at 3:00 and stops at 7:00.

I'm seeing examples with class Service , but I'm not sure how to launch it and stop it at the time I want.

public class AutoIndexadorService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new IndexerMasterViewsTask().execute();
        new IndexerWorkViewsTask().execute();
        return super.onStartCommand(intent, flags, startId);
    }
}

The AsyncTask

  

IndexerMasterViewsTask (). execute ();

and

  

IndexerWorkViewsTask (). execute ()

must be constantly running during this period, from 3:00 a.m. to 7:00 p.m.

Can someone help me?

    
asked by JoCuTo 18.06.2018 в 17:52
source

1 answer

0

I would recommend using the alarm service to be able to launch certain events every so often

is called AlarmManager

to be able to use it is simple, you can start it in your onCreate() of the first activity that the user goes through, then when the app starts, the service is already programmed to run

// Verificamos si la tarea ya ha sido programada. Si no, entonces crea la tarea en AlarmManager.
boolean alarmUp = (PendingIntent.getBroadcast(this, 0, 
        new Intent("com.example.dummy.AlarmReceiver"), 
        PendingIntent.FLAG_NO_CREATE) != null);

if (  !alarmUp) {
    Intent intent = new Intent("com.example.dummy.AlarmReceiver");
    intent.putExtra("activate", true);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
                                          intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);   
    calendar.set(Calendar.MINUTE, 1);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 7);   
    calendar.set(Calendar.MINUTE, 0);

    alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1, 
                  intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);          

}

So, what we are doing above is the query of a PendingIntent that matches our class name Activity that will be executed at the scheduled time. The PendingIntent.FLAG_NO_CREATE flag is important; otherwise, if you do not find our class programmed as PendingIntent, it will create it automatically. If the task is not yet scheduled, then we move forward and create it.

And then we need a class that we will call AlarmReceiver that will be the one that will be executed when it reaches that certain time, remember that this service runs in the background and although minimizes or dunks the app will launch the same

public class AlarmReceiver extends BroadcastReceiver { 

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

        if (!intent.hasExtra("activate")) {
            new IndexerMasterViewsTask().execute();
    new IndexerWorkViewsTask().execute();

        }

        /// your processing code comes here

    }
}

If you find the extra activate that happened when we created the new alarm, it will launch the two asynctasks

remember that to set the time you set it here

calendar.set(Calendar.HOUR_OF_DAY, 0);   
    calendar.set(Calendar.MINUTE, 1);
    calendar.set(Calendar.SECOND, 0);

I hope you serve

    
answered by 18.06.2018 / 23:07
source