How long is the "JobService execution time limit" mentioned in Android's JobIntentService docs?
Asked Answered
B

3

14

While converting an app to be ready for Android Oreo, I read the docs on JobIntentService over here.

In there I find (important part emphasised):

When running as a pre-O service, the normal service execution semantics apply: [...] When running as a Job, the typical JobService execution time limit will apply, after which the job will be stopped (cleanly, not by killing the process) and rescheduled to continue its execution later.

If I look at the documented limitations there is no word about any execution time limits. Also JobScheduler does not mention anything.

  • Is this a time limit I should simply not be concerned about?
  • Is it undocumented?
  • Or is the execution time limit not/no longer existing?
  • Or will I have to redesign my services in a way that they can be interrupted and restarted at any given point in time? Best practices?
Berezina answered 5/2, 2018 at 20:5 Comment(0)
C
14

How long is the “JobService execution time limit” mentioned in Android's JobIntentService docs?

In practice, it seems to be 10 minutes. I originally determined that by testing, but IIRC somebody pointed out the limit in the source code.

Is this a time limit I should simply not be concerned about?

If you are really sure that your work will be done in less time, yes, at least for the time being.

Is it undocumented?

Yes.

Or is the execution time limit not/no longer existing?

It existed the last time I tested it.

Or will I have to redesign my services in a way that they can be interrupted and restarted at any given point in time?

Well, ideally, yes, particularly if you are using any constraints beyond time. For example, if you say that your job requires a network connection, and the device loses connectivity, your job will be stopped. That could occur well before the 10-minute time period elapses.

Best practices?

Avoid periodic background work to the greatest extent possible.

Corbitt answered 5/2, 2018 at 20:16 Comment(3)
The short version of this answer for coders familiar with iOS: about the same restrictions apply. :-)Berezina
As per online documentation, after typical JobService execution time limit, the job will be stopped (cleanly, not by killing the process) and rescheduled to continue its execution later.Since it says rescheduled to CONTINUE it's execution later, I would assume any job which is beyond 10 mins will also be respectfully finished, though in slotsWhole
I don't understand a definition "stopped (cleanly, not by killing the process)", if you are doing some operation like syncing data from the server and storing it to the database, how you will be clearly informed that you should stop doing it? If you will have forech cycle there, there is no way how to stop it cleanly.Shoer
U
1

In order to avoid execution time limit i use bellow practice it solve my problem.

@Override
    public void onDestroy() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isStopped()) {
           //do nothing
        } else {
            super.onDestroy();
           //on destroy called
        }
    }

    @Override
    public boolean onStopCurrentWork() {
        return false;
    }
Unlovely answered 8/8, 2019 at 9:41 Comment(0)
A
0

The mechanism behind this changed over time, as the job scheduler API evolved. As of 2024 the documentation on this had been improved, and is quite clear now in JobScheduler docs:

In Android version Build.VERSION_CODES.LOLLIPOP, jobs had a maximum execution time of one minute. Starting with Android version Build.VERSION_CODES.M and ending with Android version Build.VERSION_CODES.R, jobs had a maximum execution time of 10 minutes. Starting from Android version Build.VERSION_CODES.S, jobs will still be stopped after 10 minutes if the system is busy or needs the resources, but if not, jobs may continue running longer than 10 minutes.

In AOSP 11 for example the threshold was 10 minutes, as defined in frameworks/base/apex/jobscheduler/service/java/com/android/server/job/JobServiceContext.java source code:

public static final long EXECUTING_TIMESLICE_MILLIS = 10 * 60 * 1000;  // 10mins.
Armindaarming answered 17/8 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.