我已经通过在我的Android应用程序中实现了Evernote Android作业
implementation 'com.evernote:android-job:1.2.6'
我已经定义为signleton以获得实例,我已经通过在应用程序类中启动了它
JobManager.create(this).addJobCreator(new CreatingJob());
我有两节课就业创造类
public class CreatingJob implements JobCreator {
@Nullable
@Override
public Job create(@NonNull String tag) {
switch (tag) {
case SyncMasterDataJOB.TAG:
return new SyncMasterDataJOB();
}
return null;
}
}
作业类别
public class SyncMasterDataJOB extends Job {
public static final String TAG = "job_note_sync";
@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
//Doing my Task HERE
MyLog.ShowELog("JOB STARTED", "Job Has been Started");
MyToast.Lmsg(getContext(), "Job Has been Started");
return Result.SUCCESS;
}
public static void scheduleJob() {
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(SyncMasterDataJOB.TAG);
if (!jobRequests.isEmpty()) {
return;
}
new JobRequest.Builder(SyncMasterDataJOB.TAG)
.setPeriodic(MIN_INTERVAL, MIN_FLEX)
.build()
.schedule();
}
}
但问题是我的onRunJob((方法从未被调用。我是Android工作的新手。有人能告诉我哪里做错了吗?
我在这里参考
作业创建者类->
public class CreateJob implements JobCreator {
private Context context;
public CreateJob(Context context){
this.context = context;
}
// Here we have to register each of the jobs...
@Nullable
@Override
public Job create(@NonNull String tag) {
switch (tag) {
case CurrentWeatherUpdateJob.TAG:
return new CurrentWeatherUpdateJob();
default:
return null;
}
}
}
这是我注册JobCreator的地方。
// To use StartingPoint Anywhere in our app
// else you have to Instantiate StartingPoint inside every Activities on create...
public class StartingPoint extends Application {
@Override
public void onCreate() {
super.onCreate();
// Create Job is a class that registers all the Jobs...
JobManager.create(this).addJobCreator(new CreateJob(getApplicationContext()));
}
}
这是你的工作子类->
public class CurrentWeatherUpdateJob extends Job {
public static final String TAG = "CurrentWeatherUpdateJob";
// Update Weather Data every 15 Minutes...
private static final int CURRENTWEATHERUPDATE_TIMEINTERVAL = 15 * 60 * 1000;
// Interface that provides Data...
private ApiInterface service;
// For Celcius - metric / Kelvin - imperial
private String UnitType = "metric";
public CurrentWeatherUpdateJob() {
service = APIClient.getRetrofit_Weather().create(ApiInterface.class);
}
private static void ScheduleJobEvery15Minutes() {
// Scheduling Job After every 15 minutes...
new JobRequest.Builder(TAG)
.setPeriodic(CURRENTWEATHERUPDATE_TIMEINTERVAL)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequirementsEnforced(true)
.setUpdateCurrent(true)
.build()
.schedule();
}
// implement your onRunJob method here
}
从您想要的活动中调用ScheduleJobEvery15Minutes((方法。
问题不在代码中,而是在作业周期的设置中。evernote仅适用于周期性作业>=15分钟,而您使用1分钟作为运行作业的周期。请参阅evote作业的文档,因为它基于作业调度器,该调度器对运行周期性作业具有相同的约束。
private void schedulePeriodicJob() {
int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
.setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5))
.build()
.schedule();
}
这是他们放在图书馆文档中的代码。请检查一下。->
为什么周期性作业的间隔不能小于15分钟这个库是3个不同API的子集。由于Android Nougat,定期作业的最小间隔是15分钟。尽管Nougat之前的设备支持较小的间隔,但选择最不常见的设备作为该库的最小值,以便在所有设备上以相同的频率运行定期作业。
带有Android Nougat的JobScheduler允许设置较小的间隔,但该值会自动调整,并记录警告。这个库会抛出一个异常,这样行为不端的作业就会被提前捕获。你可以在这里阅读更多关于它的信息。
这是适用于我的代码->
其中//每15分钟更新一次天气数据。。。
private static final int CURRENTWEATHERUPDATE_TIMEINTERVAL = 15 * 60 * 1000;
private static void ScheduleJobEvery15Minutes() {
// Scheduling Job After every 15 minutes...
new JobRequest.Builder(TAG)
.setPeriodic(CURRENTWEATHERUPDATE_TIMEINTERVAL)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequirementsEnforced(true)
.setUpdateCurrent(true)
.build()
.schedule();
}
EDIT->还要检查你的Jobcreator类,你会返回这样的空值,
public class CreatingJob implements JobCreator {
@Nullable
@Override
public Job create(@NonNull String tag) {
switch (tag) {
case SyncMasterDataJOB.TAG:
return new SyncMasterDataJOB();
}
return null;
}
将您的代码更改为这个->
public class CreatingJob implements JobCreator {
@Nullable
@Override
public Job create(@NonNull String tag) {
switch (tag) {
case SyncMasterDataJOB.TAG:
return new SyncMasterDataJOB();
case default:
return null;
}
}