android.app.job.JobScheduler#getAllPendingJobs ( )源码实例Demo

下面列出了android.app.job.JobScheduler#getAllPendingJobs ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android-JobScheduler   文件: MainActivity.java
/**
 * 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);
            }
        }
    }
}
 
源代码3 项目: firebase-android-sdk   文件: JobInfoScheduler.java
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;
}
 
源代码4 项目: your-local-weather   文件: MainActivity.java
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);
    }
}
 
源代码5 项目: ClassSchedule   文件: AppUtils.java
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;
}
 
源代码6 项目: intra42   文件: NotificationsJobService.java
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());
}
 
源代码7 项目: proofmode   文件: PhotosContentJob.java
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;
}
 
源代码8 项目: proofmode   文件: VideosContentJob.java
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;
}
 
源代码9 项目: android-sdk   文件: ServiceScheduler.java
@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;
}
 
源代码10 项目: android-job   文件: PlatformJobManagerRule.java
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;
}
 
源代码11 项目: CumulusTV   文件: TvBootReceiver.java
@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();
}