11

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

Salman500
  • 1,213
  • 1
  • 17
  • 35
  • 1
    I've been struggling with the same issue, however, I'm using a content URI as a trigger. The issue has occurred on both devices I test with, but if I build the application in an emulated environment it only runs once. – Matthew White Feb 20 '18 at 06:47

3 Answers3

4

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.

Rob
  • 26,989
  • 16
  • 82
  • 98
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
2

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;
VollNoob
  • 267
  • 4
  • 15
  • how can i run my job service forever – Salman500 Nov 30 '17 at 11:14
  • do you know any documentation for Firebase JobDispatcher not from github and medium any other resource thnx for the answer @VollyNoob – Salman500 Nov 30 '17 at 11:16
  • 0,60 is also running two times TestingJob: onStartJob Testing Job: Thu Nov 30 16:25:27 GMT+05:00 2017 TestingJob: onStartJob: i = 0 TestingJob: onStartJob Testing Job: Thu Nov 30 16:25:27 GMT+05:00 2017 TestingJob: onStartJob: i = 0 – Salman500 Nov 30 '17 at 11:28
  • That's weird, for me it was working. Sorry, I don't know any other documentation for that – VollNoob Dec 07 '17 at 08:10
1

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.