Evernote Android_JOB onRunJob() 方法从未被调用

Evernote Android_JOB onRunJob() method is never called

我已经通过

在我的 android 应用程序中实现了 Evernote Android 作业
implementation 'com.evernote:android-job:1.2.6'

并且我已经定义为 signleton 来获取实例,我已经在我的应用程序 class 到

中启动了它
 JobManager.create(this).addJobCreator(new CreatingJob());

我有两个 class 创造就业机会CLASS

  public class CreatingJob implements JobCreator {
    @Nullable
    @Override
    public Job create(@NonNull String tag) {
        switch (tag) {
            case SyncMasterDataJOB.TAG:
                return new SyncMasterDataJOB();
        }
        return null;
    }
}

工作CLASS

    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 JOBS 的新手。谁能告诉我哪里做错了?

我参考了here

问题不在您的代码中,而是在作业周期的设置中。 evernote 仅适用于 >=15 分钟的周期性作业,而您使用 1 分钟作为 运行 作业的周期。请参阅 evenote-job 的文档,因为它基于 Job-scheduler,它对 运行ning 定期作业具有相同的约束。

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 允许设置更小的时间间隔,但该值会自动调整并记录警告。该库改为抛出异常,以便及早发现行为不当的作业。您可以在此处阅读更多相关信息。

这是适合我的代码 ->

where // 每 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();
    }

编辑 -> 还要检查你的 Jobcreator class,你正在像这样返回空值,

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;
            }
        }

工作创造者 class ->

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()));

    }
}

这是您的职位子class ->

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
}

从您想要的 activity 调用您的 ScheduleJobEvery15Minutes() 方法。