Java Code Examples for android.app.job.JobScheduler#getAllPendingJobs()

The following examples show how to use android.app.job.JobScheduler#getAllPendingJobs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From android-JobScheduler with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 2
Source File: RichBootReceiver.java    From androidtv-sample-inputs with Apache License 2.0 6 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 3
Source File: JobInfoScheduler.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: MainActivity.java    From your-local-weather with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 5
Source File: AppUtils.java    From ClassSchedule with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: NotificationsJobService.java    From intra42 with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: PhotosContentJob.java    From proofmode with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: VideosContentJob.java    From proofmode with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: ServiceScheduler.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: PlatformJobManagerRule.java    From android-job with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: TvBootReceiver.java    From CumulusTV with MIT License 5 votes vote down vote up
@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);
}
 
Example 12
Source File: AndroidJobScheduler.java    From android_job_scheduler with Apache License 2.0 4 votes vote down vote up
public static List<JobInfo> getAllPendingJobs(Context context) {
    JobScheduler scheduler = context.getSystemService(JobScheduler.class);
    return scheduler.getAllPendingJobs();
}