0

i have scheduled a job which will repeat every 10 sec. i have tried with job scheduler with setMinimumLatency and setOverrideDeadline but my job is not repeating. i have followed different tutorial and blogs , done everything as showed there but not working well.

my code below:

manifest:

<service android:name=".JobServiceClass"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="true"
            >
        </service>

JobServiceUtils:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void scheduleJob(Context context)
    {
        ComponentName componentName=new ComponentName(context,JobServiceClass.class);
        JobInfo.Builder jobInfo=new JobInfo.Builder(1000,componentName);
        jobInfo.setPersisted(true);
        jobInfo.setMinimumLatency(10000);
        jobInfo.setOverrideDeadline(10000);
        JobScheduler jobScheduler=(JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);



        if (jobScheduler!=null){
            jobScheduler.schedule(jobInfo.build());
        }
    }

JobServiceClass:

@Override
    public boolean onStartJob(JobParameters params) {

        Toast.makeText(getApplicationContext(),"Job service thread",Toast.LENGTH_LONG).show();
        asynctaskClass=new AsynctaskClass(getApplicationContext(), new AsynctaskResult() {
            @Override
            public void asyncTaskResult(String result) {

            }
        });
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Toast.makeText(getApplicationContext(),"Job service thread stop",Toast.LENGTH_LONG).show();
        return true;
    }

initialization from activity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            JobServiceUtils.scheduleJob(MainActivity.this);
        }else {
            Intent intent1 = new Intent(this, ServiceClass.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent1);
        }

can anyone tell me the exact thing what i am missing here

AAA
  • 485
  • 6
  • 23
  • What behaviour do you get? Are you aware that running frequent jobs is essentially impossible on a modern Android device when the screen is off and the device is not charging due to doze restrictions. – Elletlar Jul 24 '18 at 10:15
  • i am trying with Api 22, 23 and 26(oreo) but no where behaving well, just running once in everywhere – AAA Jul 24 '18 at 10:19
  • 22: No doze, so results on that device will deceive you, 23: Has doze-light that comes on about an hour after the screen is turned off, 26: Has full doze that comes on almost immediately after the screen is turned off. But running a job every 15 seconds is fortunately impossible because it would murder the battery whether it can be done while the screen is on or when the device is charging, I'm not sure. – Elletlar Jul 24 '18 at 10:34
  • i know it kills battery i am just testing if it works as my specific time or not, can you tell me how to make a service so that it will work as my specified interval. in all Api. – AAA Jul 24 '18 at 10:53
  • Have a look at this thread: [Job scheduler in android N with less then 15 minutes interval](https://stackoverflow.com/questions/39641278/job-scheduler-in-android-n-with-less-then-15-minutes-interval/47760856?noredirect=1#comment89942265_47760856). But again this solution only works when the screen is ON or the device is charging. The only component that can reliably fire that frequently on a modern Android device is AlarmManager.setAlarmClock( ). – Elletlar Jul 24 '18 at 11:13
  • will AlarmManager work reliably in oreo too? – AAA Jul 24 '18 at 11:16
  • Yes setAlarmClock() should work, but you cannot use it to start a job. The catch is that it only posts an alarm-clock alarm, but you can set a pending intent that will be fired when the user clicks on that alarm. The most frequent alarm that can be used to schedule a job is setExactAndAllowWhileIdle. But you will get delays of up to roughly 10 minutes while the device is dozieng. – Elletlar Jul 24 '18 at 11:21
  • thank you so much. the thing i am watching that job scheduler working but not my specific time nor exactly 15 min interval. – AAA Jul 24 '18 at 11:24
  • and yeah one thing onStopJob method is calling too frequently – AAA Jul 24 '18 at 11:27
  • Docs for setPeriodic sum up your issues: "Specify that this job should recur with the provided interval, not more than once per period. You have no control over when within this interval this job will be executed, only the guarantee that it will be executed at most once within this interval. Setting this function on the builder with setMinimumLatency(long) or setOverrideDeadline(long) will result in an error." – Elletlar Jul 24 '18 at 11:31
  • okay thank you so much :) – AAA Jul 24 '18 at 11:32

0 Answers0