JobScheduler onStartJob 一旦应用程序被杀死就会延迟



我不明白为什么服务在StartJob上没有在setOverrideDeadline(2000(中提到的给定时间内调用。

我正在安排这样的工作:

private void scheduleJob() {
JobInfo myJob = new JobInfo.Builder(0, new ComponentName(this, NetworkSchedulerService.class))
.setRequiresCharging(true)
.setMinimumLatency(1000)
.setOverrideDeadline(2000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(myJob);
}

作业服务片段:

public class NetworkSchedulerService extends JobService implements
ConnectivityReceiver.ConnectivityReceiverListener {
private static final String TAG = NetworkSchedulerService.class.getSimpleName();
private ConnectivityReceiver mConnectivityReceiver;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service created");
mConnectivityReceiver = new ConnectivityReceiver(this);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroyed");
}
/**
* When the app's MainActivity is created, it starts this service. This is so that the
* activity and this service can communicate back and forth. See "setUiCallback()"
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return START_NOT_STICKY;
}

@Override
public boolean onStartJob(JobParameters params) {
Log.i(TAG, "onStartJob" + mConnectivityReceiver);
registerReceiver(mConnectivityReceiver, new IntentFilter(Constants.CONNECTIVITY_ACTION));
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.i(TAG, "onStopJob");
unregisterReceiver(mConnectivityReceiver);
return true;
}
}

清单:

<service
android:name=".NetworkSchedulerService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"/>

有人可以帮我解决这个问题吗?如果可能,请详细说明此延迟。

@Override
public boolean onStartJob(JobParameters params) {
Log.i(TAG, "onStartJob" + mConnectivityReceiver);
return true;
}

由于系统资源不可用,上述方法调用会延迟。JobSceduler 没有必要在我在上面代码中提到的给定截止日期内启动作业。

例如:设置覆盖截止日期(2000(

一些 JobInfo 方法,如 setRequiresCharge(( 和setRequiresBatteryNotLow((可以减少延迟。

注意:此延迟因牛轧糖和奥利奥设备而异。所以有必要看看他们的延迟。

尝试将启用的属性添加到清单中的 NetworkSchedulerService:

android:enabled="true"

相关内容

  • 没有找到相关文章

最新更新