0

I have developed an application whose main purpose is to read some QR codes using zxing library for it. This works fine.

My problem comes with the workmanager and scheduled jobs. I have a class where I start a job with the workmanager that is supposed to start every 24 hours at 8am. The code to stablish the start time is from this link https://stackoverflow.com/questions/50363541/schedule-a-work-on-a-specific-time-with-workmanager

long delay;
        int SELF_REMINDER_HOUR = 8;
        if (DateTime.now().getHourOfDay() < SELF_REMINDER_HOUR) {
            delay = new Duration(DateTime.now(), DateTime.now().withTimeAtStartOfDay().plusHours(SELF_REMINDER_HOUR)).getStandardMinutes();
        } else {
            delay = new Duration(DateTime.now(), DateTime.now().withTimeAtStartOfDay().plusDays(1).plusHours(SELF_REMINDER_HOUR)).getStandardMinutes();
        }
        long minPeriodicFlexMillis = PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS;//5 minutos
        long minPeriodicIntervalMillis = PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS;//15 minutos

        //Establecemos las condiciones de ejecución de la tarea programada. En este caso que haya conexión.
        Constraints condiciones = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
        PeriodicWorkRequest peticionPeriodica = new PeriodicWorkRequest.Builder(ComprobarRonda.class, 24, TimeUnit.HOURS, PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS)
                .addTag("TareaPeriodica_hora")
                .setConstraints(condiciones)
                .setInitialDelay(delay, TimeUnit.MINUTES)//para que se ejecute siempre a las 8 de la mañana
                .build();

        WorkManager workManager = WorkManager.getInstance(application);
        workManager.enqueueUniquePeriodicWork("comprobarRonda", ExistingPeriodicWorkPolicy.KEEP, peticionPeriodica);

ComprobarRonda class extends Worker class and the overrited doWork method reads the QR's stored at a realm database and send it by email.

First of all I have disabled the battery optimization for my application and enabled the auto start so that the work is launched even with the mobile locked.

Before putting the application into production, I have tested the schedule with a periodicity of 15 minutes and without delay. At first it works fine but on many occasions the frequency is greater than 15 minutes, but it works.

However, when I have started it with a periodicity of 24 hours, it has not worked and the email has not been sent.

The mobile used for the project is an alcatel 1, very basic.

UPDATE

I have run the following adb command adb shell dumpsys jobscheduler and i've notice that my scheduled job isn't satisfing my delay constraint.

JOB #u0a208/1: 7e88208 com.grupomillan.rondavigilancia/androidx.work.impl.background.systemjob.SystemJobService u0a208 tag=job/com.grupomillan.rondavigilancia/androidx.work.impl.background.systemjob.SystemJobService Source: uid=u0a208 user=0 pkg=com.grupomillan.rondavigilancia JobInfo: Service: com.grupomillan.rondavigilancia/androidx.work.impl.background.systemjob.SystemJobService Requires: charging=false batteryNotLow=false deviceIdle=false Extras: mParcelledData.dataSize=248 Network type: NetworkRequest [ NONE id=0, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Uid: 10208 AdministratorUids: [] RequestorUid: -1 RequestorPackageName: null] ] Minimum latency: +21h13m48s117ms Backoff: policy=1 initial=+30s0ms Has early constraint Required constraints: TIMING_DELAY CONNECTIVITY [0x90000000] Dynamic constraints: Satisfied constraints: CONNECTIVITY DEVICE_NOT_DOZING BACKGROUND_NOT_RESTRICTED WITHIN_QUOTA [0x13400000] Unsatisfied constraints: TIMING_DELAY [0x80000000] Doze whitelisted: true Uid: active Tracking: CONNECTIVITY TIME QUOTA Implicit constraints: readyNotDozing: true readyNotRestrictedInBg: true readyDynamicSatisfied: false Network: 122 Standby bucket: ACTIVE Enqueue time: -2m55s432ms Run time: earliest=+21h10m52s685ms, latest=none, original latest=none Restricted due to: none. Ready: false (job=false user=true !restricted=true !pending=true !active=true !backingup=true comp=true)

Why it is occurring this?

0 Answers0