1

with JobScheduler I create a job that has to run periodically every 15 min. But it only works if my app is opened. I

When I run app all queued jobs runs one after another, even after device reboot.

AndroidManifest.xml

<service
            android:name=".MyJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />

MyJobService.class

public class MyJobService extends JobService {
    private static final String TAG = "MyJobService";

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "onStartJob: job started");
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "onStopJob: job stopped");
        return false;
    }
}

And this is how I setup JobScheduler

public static void scheduleJob(Context context) {
    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(context, MyJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(1337, componentName);
    builder.setPeriodic(15 * 60 * 1000);
    builder.setPersisted(true);
    builder.build();

    if (jobScheduler.schedule(builder.build()) <= 0) {
        Logr.d(TAG, "scheduleJob: Some error while scheduling the job");
    }
}

So whats wrong here?

patonjo
  • 440
  • 4
  • 13
  • Enable autostart option under device setting or use Workmanager for scheduling task. – Keyur Thumar May 16 '19 at 07:50
  • I want to use JobScheduler, how to enable autostart option? In what settings? When I run app all queued jobs runs one after another. – patonjo May 16 '19 at 07:53
  • With newer android version, if your app is killed from background Job Scheduler will trigger this is problem. More - You can enable autostart from setting in application manage section. – Keyur Thumar May 16 '19 at 11:48
  • I dont understand what you wanna say. JobScheduler supose to run queued background jobs no matter my app is running or not, even after reboot queued job must run in specified period. – patonjo May 16 '19 at 21:16

1 Answers1

1

Call this method in activity/fragment onCreate()/onResume based on requirement.

public static void scheduleJob(Context context) {
    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(context, MyJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(1337, componentName);
    builder.setPeriodic(15 * 60 * 1000);
    builder.setPersisted(true);
    builder.build();

    if (jobScheduler.schedule(builder.build()) <= 0) {
        Logr.d(TAG, "scheduleJob: Some error while scheduling the job");
    }
}

In MyJobService Class

/**
 * When the app's activity/fragment is created, it starts this service. This is so that the
 * activity and this service can communicate back and forth. See "setUiCallback()"
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand");
    return START_NOT_STICKY;
}
 @Override
    public boolean onStartJob(JobParameters params) {
        Log.i(TAG, "onStartJob" + mConnectivityReceiver);
        registerReceiver(mConnectivityReceiver, new IntentFilter(Constants.CONNECTIVITY_ACTION));
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i(TAG, "onStopJob");
        unregisterReceiver(mConnectivityReceiver);
        return true;
    }

In fragment/activity

@Override
    protected void onStop() {
        // A service can be "started" and/or "bound". In this case, it's "started" by this Activity
        // and "bound" to the JobScheduler (also called "Scheduled" by the JobScheduler). This call
        // to stopService() won't prevent scheduled jobs to be processed. However, failing
        // to call stopService() would keep it alive indefinitely.
        stopService(new Intent(this, NetworkSchedulerService.class));
        super.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Start service and provide it a way to communicate with this class.
        Intent startServiceIntent = new Intent(this, NetworkSchedulerService.class);
        startService(startServiceIntent);
    }
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
  • 1
    But this is not helping me with my problem. When I run app all queued jobs runs one after another, even after device reboot. – patonjo May 16 '19 at 08:24