Vivek
Vivek

Reputation: 1843

Generating JobId for Android Jobscheduler services

I have multiple JobServices in my app. As per the Android docs, JobId for each JobService has to be unique per uid. In order to avoid collisions, I am using unique String hashcode as my JobId. this can lead to negative JobIds as well. Is this the right way of generating JobIds?

Upvotes: 0

Views: 1750

Answers (1)

Steve Moretz
Steve Moretz

Reputation: 3168

Okay so one way of doing it,maybe the only way(If you want complete automated numbers without using an arrayList of previous numbers) is to use a static field.So using this technique you'll have:

public class MyJobService extends JobService {
    public static int jobIb = 0;
...

    public boolean onStartJob(JobParameters jobParameters) {
        jobIb++;

now each time creating a job info just use:

new JobInfo.Builder(MyJobService.jobIb,componentName).setExtras(bundle).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build()

So this is the way to do it if you have only one type of JobService,if you have more simply call MyJobService.jobIb++; on their onStartJob as well you'll be fine.

Upvotes: 0

Related Questions