Search code examples
androidjob-schedulingjobservice

Scheduled JobService runs only when I open my app


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?


Solution

  • 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);
        }