0

我已经在我的 android 应用程序中实现了 Evernote Android Job

implementation 'com.evernote:android-job:1.2.6'

我已经定义为signleton来获取实例我已经在我的Application类中通过

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

我从这里参考

4

2 回答 2

1

工作创建者类 ->

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() 方法。

于 2018-12-07T09:00:27.100 回答
0

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

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 分钟。尽管 pre 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 类,你正在返回这样的空值,

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;
            }
        }
于 2018-12-07T07:51:13.883 回答