1

I am not getting why service onStartJob not calling with given time which mentioned in setOverrideDeadline(2000).

I am scheduling job like:

private void scheduleJob() {
    JobInfo myJob = new JobInfo.Builder(0, new ComponentName(this, NetworkSchedulerService.class))
            .setRequiresCharging(true)
            .setMinimumLatency(1000)
            .setOverrideDeadline(2000)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setPersisted(true)
            .build();

    JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(myJob);
}

JobService snippet:

public class NetworkSchedulerService extends JobService implements
        ConnectivityReceiver.ConnectivityReceiverListener {

    private static final String TAG = NetworkSchedulerService.class.getSimpleName();

    private ConnectivityReceiver mConnectivityReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Service created");
        mConnectivityReceiver = new ConnectivityReceiver(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Service destroyed");
    }

    /**
     * When the app's MainActivity 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;
    }
}

Manifest:

 <service
            android:name=".NetworkSchedulerService"
            android:exported="true"
            android:permission="android.permission.BIND_JOB_SERVICE"/>

Can somebody please help me to resolve this issue? If possible please provide detail explanation about this delay.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • could you please add your manifest? have you added the job service declaration into your manifest? – Anis BEN NSIR Oct 03 '18 at 08:40
  • @AnisBENNSIR: Please have a look at manifest. – Jitesh Mohite Oct 03 '18 at 09:54
  • from your implementation, i can see any issue. Is your job not running at all or it run after 1s or you are expecting that it run after 2s? Could you please detail the expected behaviour and what your are exactly facing... From your implementation your receiver will be unregistred as soon as it's registred... – Anis BEN NSIR Oct 03 '18 at 10:20
  • @AnisBENNSIR: I am expecting call after 2000 milliseconds but getting it almost 1 minute. Even my device is charged. Not getting why call gets delayed. – Jitesh Mohite Oct 04 '18 at 02:57

2 Answers2

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

The above method call gets delayed due to system resource unavailability. It is not necessary that JobSceduler will start the job within given deadline which I mentioned in the above code.

eg: setOverrideDeadline(2000)

Some of JobInfo methods like setRequiresCharging() and setRequiresBatteryNotLow() can decrease the delay.

Note: This delay varies in both Nougat and Oreo devices. So It's necessary to look at their delay.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
-1

Try adding enabled attribute to NetworkSchedulerService in Manifest:

android:enabled="true"
P. Suvajac
  • 29
  • 4