Scheduled job executes multiple time in Evernote- AndroidJob









up vote
7
down vote

favorite
2












I have one periodic job I want to run and it is implemented with the help of Evernote's Android Job library.



What I wish to achieve is to updateMyLocation ever 15 mins.



The problem is that every 15 mins, the job seems to be executing multiple times.



I tested using a OnePlus3 device and from debugging, I observed that the
LocationUpdateJob.schedule() is called only once, which is correct, but the LocationUpdateJob.onRunJob() gets called multiple times, which is incorrect, but should only be called once every 15 mins.



Additionally, illegalStateExceptions are thrown from some devices, according to crashlytics.
This particular exception only happens on Android 7 devices.



Here is the crash from crash report:



 Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfocom.mydomain.myapp/MainActivity: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:1701)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)
at com.evernote.android.job.v21.JobProxy21.schedule(SourceFile:198)
at com.evernote.android.job.v21.JobProxy21.plantPeriodic(SourceFile:92)
at com.evernote.android.job.JobManager.scheduleWithApi(SourceFile:282)
at com.evernote.android.job.JobManager.schedule(SourceFile:240)
at com.evernote.android.job.JobRequest.schedule(SourceFile:366)
at com.mydomain.myapp.service.locationUpdate.LocationUpdateJob.schedule(SourceFile:33)
at com.mydomain.myapp.activities.HubActivity.onLoginSuccess(SourceFile:173)
at com.mydomain.myapp.activities.HubActivity.onCreate(SourceFile:115)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)


Here is my code :



Applicataion class



@Override
public void onCreate()
//init things....
JobManager
.create(this)
.addJobCreator(new LocationUpdateJobCreator());



LocationUpdateJobCreator



public class LocationUpdateJobCreator implements JobCreator 

@Override
public Job create(String s)
switch (s)
case LocationUpdateJob.TAG:
return new LocationUpdateJob();
default:
return null;





MainActivity :



private void onLogin() 
// do other things...
LocationUpdateJob.schedule();



LocationUpdateJob



public class LocationUpdateJob extends Job 

public static final String TAG = "LocationUpdateJob";
private static int jobId = -1;


public static void schedule()
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, FLEX)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.build()
.schedule();



public static void stop()
JobManager
.instance()
.cancel(jobId);



@NonNull
@Override
protected Result onRunJob(Params params)
updateLocation();
return Result.SUCCESS;




I forked the Evernote's sample project, but they do the exact same steps, but I couldn't figure out what I am doing differently.










share|improve this question

















  • 1




    Has the same issue too, stackoverflow.com/q/50877452/6352712
    – Serg Burlaka
    Jun 15 at 14:45














up vote
7
down vote

favorite
2












I have one periodic job I want to run and it is implemented with the help of Evernote's Android Job library.



What I wish to achieve is to updateMyLocation ever 15 mins.



The problem is that every 15 mins, the job seems to be executing multiple times.



I tested using a OnePlus3 device and from debugging, I observed that the
LocationUpdateJob.schedule() is called only once, which is correct, but the LocationUpdateJob.onRunJob() gets called multiple times, which is incorrect, but should only be called once every 15 mins.



Additionally, illegalStateExceptions are thrown from some devices, according to crashlytics.
This particular exception only happens on Android 7 devices.



Here is the crash from crash report:



 Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfocom.mydomain.myapp/MainActivity: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:1701)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)
at com.evernote.android.job.v21.JobProxy21.schedule(SourceFile:198)
at com.evernote.android.job.v21.JobProxy21.plantPeriodic(SourceFile:92)
at com.evernote.android.job.JobManager.scheduleWithApi(SourceFile:282)
at com.evernote.android.job.JobManager.schedule(SourceFile:240)
at com.evernote.android.job.JobRequest.schedule(SourceFile:366)
at com.mydomain.myapp.service.locationUpdate.LocationUpdateJob.schedule(SourceFile:33)
at com.mydomain.myapp.activities.HubActivity.onLoginSuccess(SourceFile:173)
at com.mydomain.myapp.activities.HubActivity.onCreate(SourceFile:115)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)


Here is my code :



Applicataion class



@Override
public void onCreate()
//init things....
JobManager
.create(this)
.addJobCreator(new LocationUpdateJobCreator());



LocationUpdateJobCreator



public class LocationUpdateJobCreator implements JobCreator 

@Override
public Job create(String s)
switch (s)
case LocationUpdateJob.TAG:
return new LocationUpdateJob();
default:
return null;





MainActivity :



private void onLogin() 
// do other things...
LocationUpdateJob.schedule();



LocationUpdateJob



public class LocationUpdateJob extends Job 

public static final String TAG = "LocationUpdateJob";
private static int jobId = -1;


public static void schedule()
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, FLEX)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.build()
.schedule();



public static void stop()
JobManager
.instance()
.cancel(jobId);



@NonNull
@Override
protected Result onRunJob(Params params)
updateLocation();
return Result.SUCCESS;




I forked the Evernote's sample project, but they do the exact same steps, but I couldn't figure out what I am doing differently.










share|improve this question

















  • 1




    Has the same issue too, stackoverflow.com/q/50877452/6352712
    – Serg Burlaka
    Jun 15 at 14:45












up vote
7
down vote

favorite
2









up vote
7
down vote

favorite
2






2





I have one periodic job I want to run and it is implemented with the help of Evernote's Android Job library.



What I wish to achieve is to updateMyLocation ever 15 mins.



The problem is that every 15 mins, the job seems to be executing multiple times.



I tested using a OnePlus3 device and from debugging, I observed that the
LocationUpdateJob.schedule() is called only once, which is correct, but the LocationUpdateJob.onRunJob() gets called multiple times, which is incorrect, but should only be called once every 15 mins.



Additionally, illegalStateExceptions are thrown from some devices, according to crashlytics.
This particular exception only happens on Android 7 devices.



Here is the crash from crash report:



 Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfocom.mydomain.myapp/MainActivity: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:1701)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)
at com.evernote.android.job.v21.JobProxy21.schedule(SourceFile:198)
at com.evernote.android.job.v21.JobProxy21.plantPeriodic(SourceFile:92)
at com.evernote.android.job.JobManager.scheduleWithApi(SourceFile:282)
at com.evernote.android.job.JobManager.schedule(SourceFile:240)
at com.evernote.android.job.JobRequest.schedule(SourceFile:366)
at com.mydomain.myapp.service.locationUpdate.LocationUpdateJob.schedule(SourceFile:33)
at com.mydomain.myapp.activities.HubActivity.onLoginSuccess(SourceFile:173)
at com.mydomain.myapp.activities.HubActivity.onCreate(SourceFile:115)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)


Here is my code :



Applicataion class



@Override
public void onCreate()
//init things....
JobManager
.create(this)
.addJobCreator(new LocationUpdateJobCreator());



LocationUpdateJobCreator



public class LocationUpdateJobCreator implements JobCreator 

@Override
public Job create(String s)
switch (s)
case LocationUpdateJob.TAG:
return new LocationUpdateJob();
default:
return null;





MainActivity :



private void onLogin() 
// do other things...
LocationUpdateJob.schedule();



LocationUpdateJob



public class LocationUpdateJob extends Job 

public static final String TAG = "LocationUpdateJob";
private static int jobId = -1;


public static void schedule()
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, FLEX)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.build()
.schedule();



public static void stop()
JobManager
.instance()
.cancel(jobId);



@NonNull
@Override
protected Result onRunJob(Params params)
updateLocation();
return Result.SUCCESS;




I forked the Evernote's sample project, but they do the exact same steps, but I couldn't figure out what I am doing differently.










share|improve this question













I have one periodic job I want to run and it is implemented with the help of Evernote's Android Job library.



What I wish to achieve is to updateMyLocation ever 15 mins.



The problem is that every 15 mins, the job seems to be executing multiple times.



I tested using a OnePlus3 device and from debugging, I observed that the
LocationUpdateJob.schedule() is called only once, which is correct, but the LocationUpdateJob.onRunJob() gets called multiple times, which is incorrect, but should only be called once every 15 mins.



Additionally, illegalStateExceptions are thrown from some devices, according to crashlytics.
This particular exception only happens on Android 7 devices.



Here is the crash from crash report:



 Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfocom.mydomain.myapp/MainActivity: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:1701)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)
at com.evernote.android.job.v21.JobProxy21.schedule(SourceFile:198)
at com.evernote.android.job.v21.JobProxy21.plantPeriodic(SourceFile:92)
at com.evernote.android.job.JobManager.scheduleWithApi(SourceFile:282)
at com.evernote.android.job.JobManager.schedule(SourceFile:240)
at com.evernote.android.job.JobRequest.schedule(SourceFile:366)
at com.mydomain.myapp.service.locationUpdate.LocationUpdateJob.schedule(SourceFile:33)
at com.mydomain.myapp.activities.HubActivity.onLoginSuccess(SourceFile:173)
at com.mydomain.myapp.activities.HubActivity.onCreate(SourceFile:115)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)


Here is my code :



Applicataion class



@Override
public void onCreate()
//init things....
JobManager
.create(this)
.addJobCreator(new LocationUpdateJobCreator());



LocationUpdateJobCreator



public class LocationUpdateJobCreator implements JobCreator 

@Override
public Job create(String s)
switch (s)
case LocationUpdateJob.TAG:
return new LocationUpdateJob();
default:
return null;





MainActivity :



private void onLogin() 
// do other things...
LocationUpdateJob.schedule();



LocationUpdateJob



public class LocationUpdateJob extends Job 

public static final String TAG = "LocationUpdateJob";
private static int jobId = -1;


public static void schedule()
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, FLEX)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.build()
.schedule();



public static void stop()
JobManager
.instance()
.cancel(jobId);



@NonNull
@Override
protected Result onRunJob(Params params)
updateLocation();
return Result.SUCCESS;




I forked the Evernote's sample project, but they do the exact same steps, but I couldn't figure out what I am doing differently.







android cron-task evernote android-jobscheduler






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 11 '17 at 18:11









rgv

7431931




7431931







  • 1




    Has the same issue too, stackoverflow.com/q/50877452/6352712
    – Serg Burlaka
    Jun 15 at 14:45












  • 1




    Has the same issue too, stackoverflow.com/q/50877452/6352712
    – Serg Burlaka
    Jun 15 at 14:45







1




1




Has the same issue too, stackoverflow.com/q/50877452/6352712
– Serg Burlaka
Jun 15 at 14:45




Has the same issue too, stackoverflow.com/q/50877452/6352712
– Serg Burlaka
Jun 15 at 14:45












1 Answer
1






active

oldest

votes

















up vote
12
down vote



accepted










I found the answer to the whole thing.
I am answering this, incase anyone else faces the same issue.



Ok, so according to my logic above, this is what was happening :



1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.



2) User closes and opens the app again, another job is scheduled in the OS.



3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).



So, what did I do to solve this ?



I modified my schedule() to look like the following :



public static void schedule() 
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();



What is different here, from the old way I scheduled is : .setUpdateCurrent(true)



So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.



There is a very brief and good explanation here, please read it.






share|improve this answer


















  • 2




    There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
    – Alessio
    Mar 17 at 12:41










  • That's a great point to know, Thank you!
    – rgv
    Mar 18 at 15:14






  • 1




    This discussion helped me a lot ..+1
    – BENN1TH
    Apr 30 at 10:05










  • is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
    – Aman Verma
    Jun 7 at 11:47










  • I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
    – rgv
    Jun 7 at 19:03










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46161930%2fscheduled-job-executes-multiple-time-in-evernote-androidjob%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
12
down vote



accepted










I found the answer to the whole thing.
I am answering this, incase anyone else faces the same issue.



Ok, so according to my logic above, this is what was happening :



1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.



2) User closes and opens the app again, another job is scheduled in the OS.



3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).



So, what did I do to solve this ?



I modified my schedule() to look like the following :



public static void schedule() 
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();



What is different here, from the old way I scheduled is : .setUpdateCurrent(true)



So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.



There is a very brief and good explanation here, please read it.






share|improve this answer


















  • 2




    There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
    – Alessio
    Mar 17 at 12:41










  • That's a great point to know, Thank you!
    – rgv
    Mar 18 at 15:14






  • 1




    This discussion helped me a lot ..+1
    – BENN1TH
    Apr 30 at 10:05










  • is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
    – Aman Verma
    Jun 7 at 11:47










  • I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
    – rgv
    Jun 7 at 19:03














up vote
12
down vote



accepted










I found the answer to the whole thing.
I am answering this, incase anyone else faces the same issue.



Ok, so according to my logic above, this is what was happening :



1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.



2) User closes and opens the app again, another job is scheduled in the OS.



3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).



So, what did I do to solve this ?



I modified my schedule() to look like the following :



public static void schedule() 
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();



What is different here, from the old way I scheduled is : .setUpdateCurrent(true)



So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.



There is a very brief and good explanation here, please read it.






share|improve this answer


















  • 2




    There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
    – Alessio
    Mar 17 at 12:41










  • That's a great point to know, Thank you!
    – rgv
    Mar 18 at 15:14






  • 1




    This discussion helped me a lot ..+1
    – BENN1TH
    Apr 30 at 10:05










  • is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
    – Aman Verma
    Jun 7 at 11:47










  • I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
    – rgv
    Jun 7 at 19:03












up vote
12
down vote



accepted







up vote
12
down vote



accepted






I found the answer to the whole thing.
I am answering this, incase anyone else faces the same issue.



Ok, so according to my logic above, this is what was happening :



1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.



2) User closes and opens the app again, another job is scheduled in the OS.



3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).



So, what did I do to solve this ?



I modified my schedule() to look like the following :



public static void schedule() 
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();



What is different here, from the old way I scheduled is : .setUpdateCurrent(true)



So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.



There is a very brief and good explanation here, please read it.






share|improve this answer














I found the answer to the whole thing.
I am answering this, incase anyone else faces the same issue.



Ok, so according to my logic above, this is what was happening :



1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.



2) User closes and opens the app again, another job is scheduled in the OS.



3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).



So, what did I do to solve this ?



I modified my schedule() to look like the following :



public static void schedule() 
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();



What is different here, from the old way I scheduled is : .setUpdateCurrent(true)



So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.



There is a very brief and good explanation here, please read it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 24 at 19:27

























answered Sep 11 '17 at 20:01









rgv

7431931




7431931







  • 2




    There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
    – Alessio
    Mar 17 at 12:41










  • That's a great point to know, Thank you!
    – rgv
    Mar 18 at 15:14






  • 1




    This discussion helped me a lot ..+1
    – BENN1TH
    Apr 30 at 10:05










  • is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
    – Aman Verma
    Jun 7 at 11:47










  • I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
    – rgv
    Jun 7 at 19:03












  • 2




    There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
    – Alessio
    Mar 17 at 12:41










  • That's a great point to know, Thank you!
    – rgv
    Mar 18 at 15:14






  • 1




    This discussion helped me a lot ..+1
    – BENN1TH
    Apr 30 at 10:05










  • is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
    – Aman Verma
    Jun 7 at 11:47










  • I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
    – rgv
    Jun 7 at 19:03







2




2




There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
– Alessio
Mar 17 at 12:41




There's only one side effect of setUpdateCurrent(true) which doesn't work for me: it re-schedules the new job starting from now. So if say your recurrent job happens every 24 hours, but the app is killed twice a day constantly by the user, the re-scheduled job will never run. So I solved this issue with a strict guard for the same scheduled job using JobManager.instance().getAllJobRequestsForTag(tag), while using setUpdateCurrent(true) otherwise.
– Alessio
Mar 17 at 12:41












That's a great point to know, Thank you!
– rgv
Mar 18 at 15:14




That's a great point to know, Thank you!
– rgv
Mar 18 at 15:14




1




1




This discussion helped me a lot ..+1
– BENN1TH
Apr 30 at 10:05




This discussion helped me a lot ..+1
– BENN1TH
Apr 30 at 10:05












is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
– Aman Verma
Jun 7 at 11:47




is it possible to run both the jobs chronologically? i have this use case where i have to update data to server in background 60 seconds after a Transaction(Monetary transaction). So suppose i make a first transaction and schedule a job to run after 60 seconds and now within these 60 seconds i make another transaction and for this i scheduled another job but with the same tag. i found out that the second override the first and it somehow makes me think of any enqueue functionality to be embedded in this Library if there is none. is there?
– Aman Verma
Jun 7 at 11:47












I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
– rgv
Jun 7 at 19:03




I think this should be a separate question, but, the reason why it's being over ridden is, if you crate the jobs with same tag and setUpdate(true), it will get replaced
– rgv
Jun 7 at 19:03

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46161930%2fscheduled-job-executes-multiple-time-in-evernote-androidjob%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo