Schedule recurring job using firebase job dispatcher

扶醉桌前 提交于 2019-12-12 08:22:39

问题


I am trying to post the location of the android device to server every 10 minutes. I am using firebase job dispatcher to do this

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
    .setService(UpdateLocationService.class)
    .setRecurring(true)
    .setTrigger(Trigger.executionWindow(10, 20))
    .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
    .setTag("location-update-job")
    .setLifetime(Lifetime.FOREVER)
    .build();
dispatcher.mustSchedule(myJob);

UpdateLocationService gets the location and sends to server.

My problem: Things are mostly working fine. Only thing is, the jobs are getting scheduled with a difference of 4m, 6m, 7m, 8m, 10m, 16m, 23m...

Can some one please help me understand going on.

Update: I want the location once in 10-20 minutes. In the above code, the value is too low just for the testing purposes


回答1:


Also the:

Trigger.executionWindow(windowStart, windowEnd)

expects the windowStart and windowEnd in seconds. As per your requirement, you want the window to be 10 mins. So you should use something like:

Trigger.executionWindow(10*60, 20*60)



回答2:


There are a few reasons why this could be happening. Firstly is your job returning false in onStopJob()? From the docs

@Override
public boolean onStopJob(JobParameters job) {
    return false; // Answers the question: "Should this job be retried?"
}

If the job needs be retried then the backoff will be applied. Combine this with the fact you want it to run again every 10-20 seconds you might get the results you are experiencing.

You have not set any constraints for the job, which also will affect when it will run. e.g.

.setConstraints( // only run on an unmetered network Constraint.ON_UNMETERED_NETWORK, // only run when the device is charging Constraint.DEVICE_CHARGING )

Furthermore, I would not use a scheduled job for what you are doing. Look at the Google API Client which offers periodic updates from the fused location provider.

You can implement a callback on your Service or Activity like so

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
    ...
    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }

    private void updateUI() {
        mLatitudeTextView.setText(String.valueOf(mCurrentLocation.getLatitude()));
        mLongitudeTextView.setText(String.valueOf(mCurrentLocation.getLongitude()));
        mLastUpdateTimeTextView.setText(mLastUpdateTime);
    }
}

Checkout the full docs here but I believe you will have a more consistent experience with services dedicated to what you are trying to achieve.

https://developer.android.com/training/location/receive-location-updates.html




回答3:


ExecutionWindow specifies approximate time. It's not guaranteed that job will run at the given window. If it misses the window the job will run at earliest time later under ideal circumstances.For recurring jobs once the job has finished next job will calculate execution window time from the time job last run.

LINK

ExecutionWindow represents a Job trigger that becomes eligible once the current elapsed time exceeds the scheduled time + the {@code windowStart} value. The scheduler backend is encouraged to use the windowEnd value as a signal that the job should be run, but this is not an enforced behavior.



来源:https://stackoverflow.com/questions/40357445/schedule-recurring-job-using-firebase-job-dispatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!