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