0

I am learning how to use JobScheduler. as shown in onresume method, I set the criteria to be met in order to execute the job, the job will be scheduled when the device is not charging, no matter the device is idle or not and the job will be executed every 7 seconds.

at run time, the usb cable is connected to the device in order to install the App which means the device is charging, so after installing the App the job have not started because the device is charging, but after i unplug the usb cable i exected the job to be executed but what happened is that the job never started, and i could not understand why

please let me know why such behavior is happeneing and please let me know the answer of the following question it will help me to better understand the jobScheduler:

Q: is setRequiresCharging(false) means, that the task will be executed only if the device is NOT charging or it means that the task will be executed no matter if the device is charging or not?

main activity

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static int jobId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate");
    setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
    super.onResume();
    Log.w(TAG, "onResume");

    ComponentName serviceComponent = new ComponentName(this, MyJobService.class);
    JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent);

    builder.setRequiresCharging(false);
    builder.setRequiresDeviceIdle(false);
    builder.setPeriodic(7 * 1000);

    JobScheduler jobScheduler = (JobScheduler) getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(builder.build());
}

}

jobService:

package example.com.jobscheduler_00;

public class MyJobService extends JobService {
private static final String TAG = MyJobService.class.getSimpleName();

@Override
public boolean onStartJob(JobParameters params) {
    Log.w(TAG, "onStartJob JobId: " + params.getJobId());
    Toast.makeText(this, "onStartJob JobId:" + params.getJobId(), Toast.LENGTH_SHORT).show();

    jobFinished(params, false);
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    Log.w(TAG, "onStopJob");
    Toast.makeText(this, "onStopJob JobId:" + params.getJobId(), Toast.LENGTH_SHORT).show();

    return true;
}

}

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • "and the job will be executed every 7 seconds" -- the minimum period on Android 7.0+ is 15 minutes. I do not recall the minimum period on Android 5.0-6.0, but I would be surprised if it is less than a minute. – CommonsWare Mar 25 '17 at 21:25
  • thanks sir..but as you said " I do not recall the minimum period on Android 5.0-6.0"..is there any other alternative? – Amrmsmb Mar 25 '17 at 21:46

2 Answers2

0

Is setRequiresCharging(false) means, that the task will be executed only if the device is NOT charging or it means that the task will be executed no matter if the device is charging or not?

From the documentation:

Specify that to run this job, the device needs to be plugged in. This defaults to false.

In other words, if you want your job to be run only in condition, when the device is charging - you should pass true. By default it is false, which means charging criteria is disregarded, i.e. your job will be executed regardless the device is charging or no (assuming other criterias are fulfilled).

You may check whether your job has started successfully by the int value that JobScheduler.schedule(JobInfo job) returns. It will return either RESULT_SUCCESS or RESULT_FAILURE.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • 1
    but would you please tell me why after unplugging the usb cable the scheduled job does not start immediately?? – Amrmsmb Mar 26 '17 at 10:29
  • System fill fire your job, when it finds it's a convenient time to do that. If you want exactness, you have to go with AlarmManager. – azizbekian Mar 26 '17 at 10:33
-1

The new WorkManager API will help you set necessary constraints for the task you want to schedule.

Have a look at this video for a brief intro - https://www.youtube.com/watch?v=pErTyQpA390 (WorkManager at 21:44).

EDIT: Adding an example to showcase the capabilities of the new API

For eg:

You can set constraints related to charging state of the device like this (along with other constraints like if the device is supposed to be idle for the task to run etc..)

// Create a Constraints that defines when the task should run
Constraints yourConstraints = new Constraints.Builder()
    .setRequiresDeviceIdle(true/false)
    .setRequiresCharging(true/false)
    // Many other constraints are available
     .build();

// ...then create a OneTimeWorkRequest that uses those constraints
OneTimeWorkRequest yourWork =
                new OneTimeWorkRequest.Builder(YourWorkerClass.class)
     .setConstraints(yourConstraints)
     .build();
Annsh Singh
  • 435
  • 5
  • 12
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19686955) – mlinth May 10 '18 at 07:47
  • @mlinth Thanks for the review. Let me add on to my answer to improve it. – Annsh Singh May 10 '18 at 08:12