2

What happens to a recurring job that is not finished due to a crash or Android process cleaning? In my opinion, it is not possible to reschedule it. It will never be executed again.

Example

public class MyJobService extends JobService {


    @Override
    public boolean onStartJob(JobParameters job) {
    
        doSomeWork(job);

        return true; // true = there is still work going on!
    }

    private void doSomeWork(final JobParameters job) {

        new Thread() {

            public void run() {
            
                // do some work, maybe load data from internet
                // loading...
                // loading...
                // app crashes or its process is killed by Android
                
                // this piece of code is never reached!
                jobFinished(job, false);
            
            }
        }.start();
    
    }
    
}

As you can see in the code, the job can never be terminated by the crash. It will not be rescheduled afterwards.

If you want to reschedule it with FirebaseJobDispatcher.mustSchedule(), this appears in logcat:

E/FJD. ExternalReceiver: Received execution request for already running job

W/FJD. JobService: Job with tag = ... was already running

even if you run FirebaseJobDispatcher.cancel() before.

Any suggestions?

Community
  • 1
  • 1
almisoft
  • 2,153
  • 2
  • 25
  • 33
  • In case of a crash you could call jobFinished in the catch/finally block. Also you could try getAllPendingJobs(), find your job and cancel it before scheduling a new one. – Pavlo Zin Sep 15 '17 at 11:08
  • Unfortunately, 'FirebaseJobDispatcher.getAllPendingJobs()' is not implemented. I don't mean an exception that could be catched. What happens if Android shuts down the app, e. g. in case of lack of memory?. – almisoft Sep 15 '17 at 15:03

1 Answers1

1

put your code in try catch block. in catch block call jobFinished(job, true); . "true" means the job will be rescheduled.

" What happens if Android shuts down the app, e. g. in case of lack of memory?" .In this case os will raise the OutOfMemmory error and your try catch block will handle it.

Ashik sunny
  • 56
  • 1
  • 1
  • To improve the answer maybe if you can put the code is better – Kalamarico Sep 28 '17 at 12:36
  • Again: What happens if there is no "normal" exception that could be catched? What happens if Android shuts down the app without throwing an catchable exception? – almisoft Oct 09 '17 at 10:07
  • try this onStopJob wil be called when os cancels the job, returning true will reschedule your job`@Override public boolean onStopJob(JobParameters job) { return true; }` – Ashik sunny Oct 09 '17 at 12:02
  • are you sure, `onStopJob` is always be called? It is not called e.g. when device run out of battery!? – almisoft Oct 10 '17 at 13:20
  • BatteryStatus should be handled by you. – Ashik sunny Oct 12 '17 at 12:22
  • BatteryStatus should be handled by you. according to documentation onStopJob will be called when os cancels the job for eg when required constraints are not fulflied – Ashik sunny Oct 12 '17 at 12:32