下面列出了android.app.job.JobScheduler#getAllPendingJobs ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Executed when user clicks on FINISH LAST TASK.
*/
public void finishJob(View v) {
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
List<JobInfo> allPendingJobs = jobScheduler.getAllPendingJobs();
if (allPendingJobs.size() > 0) {
// Finish the last one
int jobId = allPendingJobs.get(0).getId();
jobScheduler.cancel(jobId);
Toast.makeText(
MainActivity.this, String.format(getString(R.string.cancelled_job), jobId),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
MainActivity.this, getString(R.string.no_jobs_to_cancel),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onReceive(Context context, Intent intent) {
JobScheduler jobScheduler =
(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
// If there are not pending jobs. Create a sync job and schedule it.
List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs();
if (pendingJobs.isEmpty()) {
String inputId = context.getSharedPreferences(EpgSyncJobService.PREFERENCE_EPG_SYNC,
Context.MODE_PRIVATE).getString(EpgSyncJobService.BUNDLE_KEY_INPUT_ID, null);
if (inputId != null) {
// Set up periodic sync only when input has set up.
EpgSyncJobService.setUpPeriodicSync(context, inputId,
new ComponentName(context, SampleJobService.class));
}
return;
}
// On L/L-MR1, reschedule the pending jobs.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
for (JobInfo job : pendingJobs) {
if (job.isPersisted()) {
jobScheduler.schedule(job);
}
}
}
}
private boolean isJobServiceOn(JobScheduler scheduler, int jobId, int attemptNumber) {
for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
int existingAttemptNumber = jobInfo.getExtras().getInt(ATTEMPT_NUMBER);
if (jobInfo.getId() == jobId) {
return existingAttemptNumber >= attemptNumber;
}
}
return false;
}
private void startAlarms() {
appendLog(this, TAG, "scheduleStart at boot, SDK=", Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
JobScheduler jobScheduler = getSystemService(JobScheduler.class);
boolean scheduled = false;
for (JobInfo jobInfo: jobScheduler.getAllPendingJobs()) {
if (jobInfo.getId() > 0) {
appendLog(this, TAG, "scheduleStart does not start - it's scheduled already");
scheduled = true;
break;
}
}
if (!scheduled) {
appendLog(this, TAG, "scheduleStart at MainActivity");
AppPreference.setLastSensorServicesCheckTimeInMs(this, 0);
jobScheduler.cancelAll();
ComponentName serviceComponent = new ComponentName(this, StartAutoLocationJob.class);
JobInfo.Builder builder = new JobInfo.Builder(StartAutoLocationJob.JOB_ID, serviceComponent);
builder.setMinimumLatency(1 * 1000); // wait at least
builder.setOverrideDeadline(3 * 1000); // maximum delay
jobScheduler.schedule(builder.build());
}
} else {
Intent intentToStartUpdate = new Intent("org.thosp.yourlocalweather.action.START_ALARM_SERVICE");
intentToStartUpdate.setPackage("org.thosp.yourlocalweather");
startService(intentToStartUpdate);
}
}
private static boolean isJobPollServiceOn(Context context) {
JobScheduler scheduler = (JobScheduler) context
.getSystemService(Context.JOB_SCHEDULER_SERVICE);
for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
if (jobInfo.getId() == UPDATE_WIDGET_JOB_ID) {
return true;
}
}
return false;
}
public static void schedule(Context context) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (jobScheduler.getPendingJob(JOB_ID) != null)
return;
} else {
List<JobInfo> allPendingJobs = jobScheduler.getAllPendingJobs();
if (!allPendingJobs.isEmpty()) {
for (JobInfo j : allPendingJobs) {
if (j.getId() == JOB_ID)
return;
}
}
}
SharedPreferences settings = AppSettings.getSharedPreferences(context);
int notificationsFrequency = AppSettings.Notifications.getNotificationsFrequency(settings);
ComponentName component = new ComponentName(context, NotificationsJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, component)
.setPeriodic(60000 * notificationsFrequency);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING);
else
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
jobScheduler.schedule(builder.build());
}
public static boolean isScheduled(Context context) {
JobScheduler js = context.getSystemService(JobScheduler.class);
List<JobInfo> jobs = js.getAllPendingJobs();
if (jobs == null) {
return false;
}
for (int i=0; i<jobs.size(); i++) {
if (jobs.get(i).getId() == PHOTOS_CONTENT_JOB) {
return true;
}
}
return false;
}
public static boolean isScheduled(Context context) {
JobScheduler js = context.getSystemService(JobScheduler.class);
List<JobInfo> jobs = js.getAllPendingJobs();
if (jobs == null) {
return false;
}
for (int i=0; i<jobs.size(); i++) {
if (jobs.get(i).getId() == VIDEO_JOB_ID) {
return true;
}
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static boolean isScheduled(Context context, Integer jobId) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
for (JobInfo jobInfo : jobScheduler.getAllPendingJobs()) {
// we only don't allow rescheduling of periodic jobs. jobs for individual
// intents such as events are allowed and can end up queued in the job service queue.
if (jobInfo.getId() == jobId && jobInfo.isPeriodic()) {
return true;
}
}
}
return false;
}
public List<JobInfo> getAllPendingJobsFromScheduler() {
JobScheduler jobScheduler = getJobScheduler();
ArrayList<JobInfo> jobs = new ArrayList<>(jobScheduler.getAllPendingJobs());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Iterator<JobInfo> iterator = jobs.iterator();
while (iterator.hasNext()) {
if (iterator.next().getId() == JobIdsInternal.JOB_ID_JOB_RESCHEDULE_SERVICE) {
iterator.remove();
}
}
}
return jobs;
}
@Override
public void onReceive(Context context, Intent intent) {
JobScheduler jobScheduler =
(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
// If there are not pending jobs. Create a sync job and schedule it.
List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs();
if (pendingJobs.isEmpty()) {
String inputId = context.getSharedPreferences(EpgSyncJobService.PREFERENCE_EPG_SYNC,
Context.MODE_PRIVATE).getString(EpgSyncJobService.BUNDLE_KEY_INPUT_ID, null);
if (inputId != null) {
// Set up periodic sync only when input has set up.
EpgSyncJobService.setUpPeriodicSync(context, inputId,
new ComponentName(context, CumulusJobService.class));
}
return;
}
// On L/L-MR1, reschedule the pending jobs.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
for (JobInfo job : pendingJobs) {
if (job.isPersisted()) {
jobScheduler.schedule(job);
}
}
}
// Initialize the ChannelDatabase now
ChannelDatabase.getInstance(context);
}
public static List<JobInfo> getAllPendingJobs(Context context) {
JobScheduler scheduler = context.getSystemService(JobScheduler.class);
return scheduler.getAllPendingJobs();
}