使用firebase作业调度程序安排定期作业

kra*_*117 12 android firebase firebase-job-dispatcher

我试图每隔10分钟将android设备的位置发布到服务器.我正在使用firebase作业调度程序来执行此操作

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);
Run Code Online (Sandbox Code Playgroud)

UpdateLocationService 获取位置并发送到服务器.

我的问题:事情大多正常.唯一的问题是,这些工作的安排距离分别为4米,6米,7米,8米,10米,16米,23米......

有人可以帮我理解继续.

更新:我希望在10-20分钟内完成一次该位置.在上面的代码中,该值太低,仅用于测试目的

小智 5

还有:

Trigger.executionWindow(windowStart, windowEnd)
Run Code Online (Sandbox Code Playgroud)

期待windowStartwindowEnd在几秒钟内.根据您的要求,您希望窗口为10分钟.所以你应该使用类似的东西:

Trigger.executionWindow(10*60, 20*60)
Run Code Online (Sandbox Code Playgroud)


Gra*_*ith 4

发生这种情况的原因有几个。首先,你的工作回来false了吗onStopJob()?来自文档

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

如果需要重试作业,则将应用退避。将此与您希望它每 10-20 秒再次运行一次的事实结合起来,您可能会得到您正在经历的结果。

您尚未为作业设置任何约束,这也会影响作业的运行时间。例如

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

此外,我不会使用预定的工作来完成你正在做的事情。查看 Google API 客户端,它提供来自融合位置提供商的定期更新。

您可以像这样在您的服务或活动上实现回调

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请在此处查看完整的文档,但我相信您将通过致力于实现您想要实现的目标的服务获得更一致的体验。

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