androidfirebase-job-dispatcher

Why Firebase JobDispatcher run JobService 2 times?


As From Firebase JobDispatcher Documentation Firebase JobDispatcher

setTrigger(Trigger.executionWindow(0, 60))
// start between 0 and 60 seconds

but why my sevice running two time

Firebase JobDispacther Code

    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    Job job = dispatcher.newJobBuilder()
            .setTag("testing job")
            .setService(TestingJob.class)
            .setReplaceCurrent(true)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(0,1))
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .setLifetime(Lifetime.FOREVER)
            .build();

    dispatcher.mustSchedule(job);

Testing class (Job Service)

public class TestingJob extends JobService {

    private static final String TAG = "TestingJob";
    private int i =0;
    @Override
    public boolean onStartJob(JobParameters job) {

        Log.d(TAG, "onStartJob Testing Job: "+new Date().toString());
        Log.d(TAG, "onStartJob: i = "+String.valueOf(i));
        i+=1;
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters job) {

        Log.d(TAG, "onStopJob Testing Job: Stopped");
        return false;
    }
}

Log Cat

11-28 00:08:57.666 11793-11793: onStartJob Testing Job: Tue Nov 28 00:08:57 GMT+05:00 2017
11-28 00:08:57.666 11793-11793: onStartJob: i = 0
11-28 00:08:57.791 11793-11793: onStartJob Testing Job: Tue Nov 28 00:08:57 GMT+05:00 2017
11-28 00:08:57.791 11793-11793: onStartJob: i = 0

Manifest

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <service android:name=".services.TestingJob"
            android:exported="false">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
        </intent-filter>
    </service>

Can i use my Job Service Again mean int i should increment every time.

Thnx For Your Help


Solution

  • I think some method we should be know about it

    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
        Job job = dispatcher.newJobBuilder()
                .setTag("testing job")
                .setService(TestingJob.class)
                .setReplaceCurrent(true)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(0,1))
                .setConstraints(Constraint.ON_ANY_NETWORK)
                .setLifetime(Lifetime.FOREVER)
                .build();
    
        dispatcher.mustSchedule(job);
    

    in this you set
    .setRecurring(true) : that means repeat it continuously. set the trigger with start and end .setTrigger(Trigger.executionWindow(start, end)) :

    .setReplaceCurrent(false) : that means don't overwrite an existing job with the same tag.