Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 548
Salman500 Avatar asked Nov 27 '17 19:11

Salman500


Video Answer


3 Answers

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.

like image 96
Hemant Parmar Avatar answered Oct 21 '22 04:10

Hemant Parmar


I think the reason why it runs 2 times is your execution window.

.setTrigger(Trigger.executionWindow(0,1))

There is only a 1 second time window. Try with a broader interval, like (0, 60), that should do the trick.

To answer your other question about your variable i, you have to make it static:

private static int i = 0;
like image 2
VollNoob Avatar answered Oct 21 '22 04:10

VollNoob


Your int i will not increment every time.

setTrigger(Trigger.executionWindow(0, 60))

And this JobTrigger will determine that the created Job is now ready to execute. You have specified an execution window of 0 to 1 seconds. That's why your job is triggering in every second.

This triggering is important because recurring jobs always need an execution window trigger. Of course, if your job is not recurring, you don’t have to set any trigger for it.

Your first parameter is the interval time in seconds between 2 jobs and the second parameter is the interval time + the synchronized flextime in seconds.

like image 1
Shamsul Arafin Mahtab Avatar answered Oct 21 '22 06:10

Shamsul Arafin Mahtab