public static synchronized void syncImmediately(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnectedOrConnecting()) { Intent nowIntent = new Intent(context, QuoteIntentService.class); context.startService(nowIntent); } else { JobInfo.Builder builder = new JobInfo.Builder(ONE_OFF_ID, new ComponentName(context, QuoteJobService.class)); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setBackoffCriteria(INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_EXPONENTIAL); JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); scheduler.schedule(builder.build()); } }
static void setJobForNormalView(Context context, float pollingRate) { JobInfo.Builder builder = new JobInfo.Builder( JOB_ID_NORMAL_VIEW, new ComponentName(context.getPackageName(), JobNormalUpdateService.class.getName())) .setBackoffCriteria(15 * MINUTE, JobInfo.BACKOFF_POLICY_LINEAR) .setPeriodic((long) (pollingRate * HOUR)) .setPersisted(true); JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); if (scheduler != null) { scheduler.schedule(builder.build()); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void startJobScheduler(Context context) { if (context != null) { LogUtils.d(TAG, "KeepAliveManager--------startJobScheduler"); JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE); //开启唤醒PushService的定时任务 ComponentName componentName = new ComponentName("jack.com.servicekeep", "jack.com.servicekeep.service.KeepAliveJobSchedulerService"); //ComponentName componentName = new ComponentName(context, KeepAliveJobSchedulerService.class); ComponentName jobService = new ComponentName(context.getPackageName(), KeepAliveJobSchedulerService.class.getName()); JobInfo jobInfo = new JobInfo.Builder(JOB_ID, jobService) .setMinimumLatency(1000) //延时1s后执行 .setPersisted(true) //需要权限RECEIVE_BOOT_COMPLETED .setBackoffCriteria(TimeUnit.MINUTES.toMillis(10), JobInfo.BACKOFF_POLICY_LINEAR) //线性重试方案 .build(); int result = jobScheduler.schedule(jobInfo); if (result == RESULT_SUCCESS) { LogUtils.d(TAG, "startJobScheduler ------ success!!!"); }else{ LogUtils.d(TAG, "startJobScheduler ------ fail!!!"); } } } }
builder.setBackoffCriteria(job.getInitialBackoffMillis(), job.getBackoffPolicy());
public static void scheduleJobService(Context context) { boolean isSuccess = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, new ComponentName(context, JobSchedulerService.class)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { builder.setMinimumLatency(DaemonUtil.getIntervalTime()); builder.setOverrideDeadline(DaemonUtil.getIntervalTime() * 2); builder.setBackoffCriteria(DaemonUtil.getIntervalTime(), JobInfo.BACKOFF_POLICY_LINEAR);//线性重试方案 } else { builder.setPeriodic(DaemonUtil.getIntervalTime()); } builder.setPersisted(true); JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); if (jobScheduler != null) { jobScheduler.cancelAll(); isSuccess = jobScheduler.schedule(builder.build()) == JobScheduler.RESULT_SUCCESS; } } if (isSuccess) { Log.d(TAG, "Scheduler Success!"); } else { Log.e(TAG, "Scheduler Failed!"); } }
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static void listing11_11(Context context) { JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); ComponentName jobServiceName = new ComponentName(context, BackgroundJobService.class); // Listing 11-11: Scheduling a job with customized back-off criteria jobScheduler.schedule( new JobInfo.Builder(BACKGROUND_UPLOAD_JOB_ID, jobServiceName) // Require a network connection .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) // Require the device has been idle .setRequiresDeviceIdle(true) // Force Job to ignore constraints after 1 day .setOverrideDeadline(TimeUnit.DAYS.toMillis(1)) // Retry after 30 seconds, with linear back-off .setBackoffCriteria(30000, JobInfo.BACKOFF_POLICY_LINEAR) // Reschedule after the device has been rebooted .setPersisted(true) .build()); }
private void scheduleJob(int id, int jobId, String key) { PersistableBundle bundle = new PersistableBundle(); bundle.putInt(NotificationJobService.NOTIFICATION_MODE, id); bundle.putString(NotificationJobService.NOTIFICATION_KEY, key); JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent); if (jobId == 0) { builder.setPeriodic(120 * 1000L); } else { if (id == NotificationJobService.NOTIFICATION_POSTED_CUSTOM_UI && (!Prefs.getBoolean(Constants.PREF_DISABLE_STANDARD_NOTIFICATIONS, false))) builder.setMinimumLatency(CUSTOMUI_LATENCY); else builder.setMinimumLatency(0); builder.setBackoffCriteria(JOB_INTERVAL, JobInfo.BACKOFF_POLICY_LINEAR); builder.setOverrideDeadline(JOB_MAX_INTERVAL); builder.setExtras(bundle); } jobScheduler.schedule(builder.build()); }
private static void schedulePeriodic(Context context) { Timber.d("Scheduling a periodic task"); JobInfo.Builder builder = new JobInfo.Builder(PERIODIC_ID, new ComponentName(context, QuoteJobService.class)); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPeriodic(PERIOD) .setBackoffCriteria(INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_EXPONENTIAL); JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); scheduler.schedule(builder.build()); }