代码之家  ›  专栏  ›  技术社区  ›  Shashwat Gupta

evernote android_job onrunjob()方法从未调用

  •  0
  • Shashwat Gupta  · 技术社区  · 5 年前

    我已经在我的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()方法从未被调用。我对安卓乔布斯还不熟悉。谁能告诉我哪里做错了?

    我正在参考 here

    2 回复  |  直到 5 年前
        1
  •  1
  •   Jay Dangar Yogita Singh Chauhan    5 年前

    作业创建者类->

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

        2
  •  0
  •   Jay Dangar Yogita Singh Chauhan    5 年前

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

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

    这是他们放在图书馆文档中的代码。请看看这个。-gt;

    为什么周期性工作间隔不能小于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();
        }
    

    编辑-GT; 还要检查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;
                }
            }