Firebase Job Dispatcher doesn't trigger the service class

1.9k views Asked by At

I created static methods on an activity to call the jobscheduler Jobdispatcher doesn't dispatch the job, I guess. Please help.

The methods in class Add.java

public void add_details(String namel,String descl,String timel)
{
    add_job(timel);

}
synchronized public void add_job(String time)
{     
    Driver driver = new GooglePlayDriver(Add.this);
    FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);
    Job job = firebaseJobDispatcher.newJobBuilder()
                .setService(Addition_job.class)
                .setTag(JOB_TAG)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(5,30))
                .build();
    firebaseJobDispatcher.mustSchedule(job);
}

Addition_job.class

public class Addition_job extends JobService{
private AsyncTask Task;
@Override
public boolean onStartJob(JobParameters job) {
    Task=new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            Context context=Addition_job.this;
            Decision.notify_user(context);
            return null;
        }
    };
    return true;
}

@Override
public boolean onStopJob(JobParameters job) {
    if (Task != null) Task.cancel(true);
    return true;
}
}

Decision.java notifiy_user method

public static void notify_user(Context context)
{
    if(context!=null)
        Toast.makeText(context,"notified:",Toast.LENGTH_SHORT).show();
}

I have specified the service in the manifest with export=false and also action_Execute.

1

There are 1 answers

1
harshithdwivedi On BEST ANSWER

If you have very rigid constrain (i.e starting job exactly after some minutes or seconds) then maybe it is good idea to implement this with AlarmManager because there is no guarantee with JobDispatcher, that your task will work at the time specified, it might start to work in a few hours later.
On API 21+ Doze mode will also come into factor in deciding the time at which your task will execute.
If you want really strict scheduling - use AlarmManager.setAndAllowWhileIdle.