0

I have implemented Evernote Android Job in my android application through

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

And I have define as signleton to get instance I have initiated it in my Application class through

 JobManager.create(this).addJobCreator(new CreatingJob());

And I have two classes which are JOB CREATING CLASS

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

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


}

But the Problem is My onRunJob() method is never called. I am new to Android JOBS. Can anyone tell me where i am doing wrong?

I am Taking reference from here

Shashwat Gupta
  • 876
  • 9
  • 22

2 Answers2

1

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

this is where i am registering my 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()));

    }
}

This is your Jobs Subclass ->

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
}

Call your ScheduleJobEvery15Minutes() method from your activity you want.

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
  • When I removed Felx interval it is working fine for me. Thanks brother – Shashwat Gupta Dec 07 '18 at 09:28
  • you'r welcome, felix interval stands for running the Job after the provided felix interval. so, if you set felix interval of 5 min and period as 15 min, your job will start after 5 min and will run every 15 min. removing it will run your job immediately. – Jay Dangar Dec 07 '18 at 09:30
0

The Problem is not in your code, but in the Setting up of the Period of the Job. evernote only works with periodic Job >=15 Min, while you are using 1 minute as period for running the Job. see the Documentation of evenote-job as it's based on Job-scheduler, which has the same constraint for running periodic job.

private void schedulePeriodicJob() {
    int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5))
            .build()
            .schedule();
}

this is the code that they put on their Library Documentation. Please check this out. ->

Why can't an interval be smaller than 15 minutes for periodic jobs? This library is a subset of 3 different APIs. Since Android Nougat the minimum interval of periodic jobs is 15 minutes. Although pre Nougat devices support smaller intervals, the least common was chosen as minimum for this library so that periodic jobs run with the same frequency on all devices.

The JobScheduler with Android Nougat allows setting a smaller interval, but the value is silently adjusted and a warning is being logged. This library throws an exception instead, so that misbehaving jobs are caught early. You can read more about it here.

this is the code that works for me ->

where // Update Weather Data every 15 Minutes...

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 -> Also check your Jobcreator class, you are returning null value like this,

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

change your code to this ->

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;
            }
        }
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35