Open In App

Job Handling in Android 13

Last Updated : 07 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Google introduced a new Job Handling mechanism, unlike the previous Work Manager which was used in earlier Android Versions. For a better user experience, JobScheduler gives apps a means to designate some jobs as “prefetch” jobs. Prefetch jobs run preferably before the next app launch. In the past, the signal was exclusively employed by JobScheduler to permit prefetch jobs to opportunistically consume free or extra data. Now that JobScheduler is attempting to predict when your app will start next, it will execute any related prefetch jobs beforehand. Check that prefetch jobs are functioning as intended if you utilize them.

Prefetch jobs are conducted by the system using an estimation of when an app will launch next in Android 13 (API level 33) and higher. Any work that needs to be completed before the next app launch should be done via prefetch jobs, according to apps.

How is Job Handling different from the Work Manager?

The framework will aim to batch and delay jobs as much as possible and be intelligent about when it runs them. If a job doesn’t have a deadline, it will typically run whenever it is ready, based on the internal queue of the JobScheduler unlike what we see in the Work Manager. The system holds a wake lock for your app while a job is running. Because of this, there is little you can do to make sure the device is awake throughout the project.

Up until Android version Build.VERSION CODES.S, there was a cap of 100 jobs that could be scheduled at once. That restriction was raised to 150 as of the Android version Build.VERSION CODES.S. Jobs that are completed quickly also count toward the cap.

How to Enable Job Prefetch in Android 13

The deadlines (specified by setOverrideDeadline(long) on their prefetch jobs are not permitted for apps targeting Android version Build.VERSION CODES.TIRAMISU or later. If there is an excess of metered data available, the system may utilize this signal to loosen the network restrictions you initially specified, allowing a JobInfo to operate over a metered network. To enable app prefetch functionality in your Android App, you need to simply add this line where you schedule your job:

Java




// Add this where you schedule your jobs in your app.
// If this is set to true, it means that this job is
// intended to prefetch content that will significantly
// enhance the experience of the particular user of this
// device.
public JobInfo.Builder setPrefetch(true gfgPreFetch)


How to set priority in Job Prefetch Job Handling

The calling app will utilize the priority set here to sort jobs and apply slightly different policies based on priority. Priority will NOT be utilized as a general sorting parameter to order the jobs of various apps. Use this to tell the system which tasks it should attempt to complete before other tasks.

Java




// Use this in your activity where
// you want to prioritise your tasks
public JobInfo.Builder setPriority (int priority)


Your app will utilize the priority set here to sort jobs and apply slightly different policies based on priority. Priority will NOT be utilized as a general sorting parameter to order the jobs of various apps. Use this to tell the system which tasks it should attempt to complete before other tasks.

Why not put all jobs in high priority?

You might think in anticipation that with this priority method, you can set HIGH priority to all your apps, but that’s not the case, setting all of your tasks to high priority won’t help your app and can even make it perform worse over time. Jobs that are repeatedly retried (due to failures) will gradually have their priorities lowered in order to prevent hunger.

The job requires Internet, also need the target as Android 13

By using this strategy, you declare that network is a crucial prerequisite for your position. Your job won’t ever run if the network you specified is not available. To alter this behavior, use setOverrideDeadline(long). You usually only want to use one of these methods; calling both will override any requirements that were previously specified by setRequiredNetworkType(int).

GeekTip: You don’t need to invoke this method because the default value is null if your job does not require a network connection.

To add this component simply:

Java




// Add this to your main activity
// where you use the internet connections
public JobInfo.Builder setRequiredNetwork (NetworkRequest gfgNetworkRequest)


Conclusion

This is how you can use the Job Handler to prefetch your jobs. This indeed is a great new addition that is introduced in Android 13 and can be great if used properly, you can also configure various other parameters, like Battery Connections, or Charging State while using the Job Handler.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads