androidandroid-studiojob-schedulingandroid-job

Evernote Android_JOB onRunJob() method is never called


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


Solution

  • 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.