Java Code Examples for android.app.job.JobScheduler#RESULT_FAILURE

The following examples show how to use android.app.job.JobScheduler#RESULT_FAILURE . 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: JobSchedulerImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public int schedule(JobInfo job) {
    try {
        return mBinder.schedule(job);
    } catch (RemoteException e) {
        return JobScheduler.RESULT_FAILURE;
    }
}
 
Example 2
Source File: JobSchedulerImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public int enqueue(JobInfo job, JobWorkItem work) {
    try {
        return mBinder.enqueue(job, work);
    } catch (RemoteException e) {
        return JobScheduler.RESULT_FAILURE;
    }
}
 
Example 3
Source File: JobSchedulerImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) {
    try {
        return mBinder.scheduleAsPackage(job, packageName, userId, tag);
    } catch (RemoteException e) {
        return JobScheduler.RESULT_FAILURE;
    }
}
 
Example 4
Source File: SubmitSampleJob.java    From Auditor with MIT License 5 votes vote down vote up
static void schedule(final Context context) {
    final ComponentName serviceName = new ComponentName(context, SubmitSampleJob.class);
    final JobScheduler scheduler = context.getSystemService(JobScheduler.class);
    if (scheduler.schedule(new JobInfo.Builder(JOB_ID, serviceName)
            .setPersisted(true)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .build()) == JobScheduler.RESULT_FAILURE) {
        throw new RuntimeException("job schedule failed");
    }
}
 
Example 5
Source File: RemoteVerifyJob.java    From Auditor with MIT License 5 votes vote down vote up
static void schedule(final Context context, int interval) {
    if (interval < MIN_INTERVAL) {
        interval = MIN_INTERVAL;
        Log.e(TAG, "invalid interval " + interval + " clamped to MIN_INTERVAL " + MIN_INTERVAL);
    } else if (interval > MAX_INTERVAL) {
        interval = MAX_INTERVAL;
        Log.e(TAG, "invalid interval " + interval + " clamped to MAX_INTERVAL " + MAX_INTERVAL);
    }
    final JobScheduler scheduler = context.getSystemService(JobScheduler.class);
    final JobInfo jobInfo = scheduler.getPendingJob(PERIODIC_JOB_ID);
    final long intervalMillis = interval * 1000;
    final long flexMillis = intervalMillis / 10;
    if (jobInfo != null &&
            jobInfo.getIntervalMillis() == intervalMillis &&
            jobInfo.getFlexMillis() == flexMillis) {
        Log.d(TAG, "job already registered");
        return;
    }
    final ComponentName serviceName = new ComponentName(context, RemoteVerifyJob.class);
    if (jobInfo == null) {
        if (scheduler.schedule(new JobInfo.Builder(FIRST_RUN_JOB_ID, serviceName)
                    .setOverrideDeadline(intervalMillis - OVERRIDE_OFFSET_MS)
                    .setPersisted(true)
                    .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                    .build()) == JobScheduler.RESULT_FAILURE) {
            throw new RuntimeException("job schedule failed");
        }
    }
    if (scheduler.schedule(new JobInfo.Builder(PERIODIC_JOB_ID, serviceName)
            .setPeriodic(intervalMillis, flexMillis)
            .setPersisted(true)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .build()) == JobScheduler.RESULT_FAILURE) {
        throw new RuntimeException("job schedule failed");
    }
}
 
Example 6
Source File: JobSchedulerManager.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void startJobScheduler(String className) {
    if (AliveJobService.isLive() || isBelowLOLLIPOP()) {
        return;
    }
    jobScheduleer.cancel(JOB_ID);
    // 构建JobInfo对象,传递给JobSchedulerService
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, new ComponentName(BaseApplication.getInstance(), AliveJobService.class));
    if (Build.VERSION.SDK_INT >= 24) {
        builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); //执行的最小延迟时间
        builder.setOverrideDeadline(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);  //执行的最长延时时间
        builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
        builder.setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_LINEAR);//线性重试方案
    } else {
        builder.setPeriodic(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
    }
    builder.setPersisted(true);  // 设置设备重启时,执行该任务
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    builder.setRequiresCharging(true); // 当插入充电器,执行该任务

    // 设置设备重启时,执行该任务
    builder.setPersisted(true);
    // 当插入充电器,执行该任务
    builder.setRequiresCharging(true);
    PersistableBundle persistableBundle = new PersistableBundle();
    persistableBundle.putString(PENDING_CLASS_NAME, className);
    builder.setExtras(persistableBundle);
    JobInfo info = builder.build();
    //开始定时执行该系统任务
    int result = jobScheduleer.schedule(info);
    if (result == JobScheduler.RESULT_FAILURE) {
        CommonLogger.e("jobService启动失败");
    } else {
        CommonLogger.e("jobService启动成功");
    }
}
 
Example 7
Source File: JobSchedulingErrorListener.java    From android-migrate-to-jobs with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    JobScheduler js = (JobScheduler) appContext
            .getSystemService(Context.JOB_SCHEDULER_SERVICE);

    if (js == null) {
        Log.e(TAG, "unable to retrieve JobScheduler. What's going on?");
        return;
    }

    Log.i(TAG, "scheduling new job");
    if (js.schedule(jobToSchedule) == JobScheduler.RESULT_FAILURE) {
        Log.e(TAG, "encountered unknown error scheduling job");
    }
}
 
Example 8
Source File: JobSchedulerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public int scheduleAsPackage(JobInfo job, JobWorkItem work, int uId, String packageName,
        int userId, String tag) {
    try {
        if (ActivityManager.getService().isAppStartModeDisabled(uId,
                job.getService().getPackageName())) {
            Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString()
                    + " -- package not allowed to start");
            return JobScheduler.RESULT_FAILURE;
        }
    } catch (RemoteException e) {
    }

    synchronized (mLock) {
        final JobStatus toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());

        if (work != null && toCancel != null) {
            // Fast path: we are adding work to an existing job, and the JobInfo is not
            // changing.  We can just directly enqueue this work in to the job.
            if (toCancel.getJob().equals(job)) {

                toCancel.enqueueWorkLocked(ActivityManager.getService(), work);

                // If any of work item is enqueued when the source is in the foreground,
                // exempt the entire job.
                toCancel.maybeAddForegroundExemption(mIsUidActivePredicate);

                return JobScheduler.RESULT_SUCCESS;
            }
        }

        JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag);

        // Give exemption if the source is in the foreground just now.
        // Note if it's a sync job, this method is called on the handler so it's not exactly
        // the state when requestSync() was called, but that should be fine because of the
        // 1 minute foreground grace period.
        jobStatus.maybeAddForegroundExemption(mIsUidActivePredicate);

        if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
        // Jobs on behalf of others don't apply to the per-app job cap
        if (ENFORCE_MAX_JOBS && packageName == null) {
            if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) {
                Slog.w(TAG, "Too many jobs for uid " + uId);
                throw new IllegalStateException("Apps may not schedule more than "
                            + MAX_JOBS_PER_APP + " distinct jobs");
            }
        }

        // This may throw a SecurityException.
        jobStatus.prepareLocked(ActivityManager.getService());

        if (work != null) {
            // If work has been supplied, enqueue it into the new job.
            jobStatus.enqueueWorkLocked(ActivityManager.getService(), work);
        }

        if (toCancel != null) {
            // Implicitly replaces the existing job record with the new instance
            cancelJobImplLocked(toCancel, jobStatus, "job rescheduled by app");
        } else {
            startTrackingJobLocked(jobStatus, null);
        }
        StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED,
                uId, null, jobStatus.getBatteryName(),
                StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__SCHEDULED,
                JobProtoEnums.STOP_REASON_CANCELLED);

        // If the job is immediately ready to run, then we can just immediately
        // put it in the pending list and try to schedule it.  This is especially
        // important for jobs with a 0 deadline constraint, since they will happen a fair
        // amount, we want to handle them as quickly as possible, and semantically we want to
        // make sure we have started holding the wake lock for the job before returning to
        // the caller.
        // If the job is not yet ready to run, there is nothing more to do -- we are
        // now just waiting for one of its controllers to change state and schedule
        // the job appropriately.
        if (isReadyToBeExecutedLocked(jobStatus)) {
            // This is a new job, we can just immediately put it on the pending
            // list and try to run it.
            mJobPackageTracker.notePending(jobStatus);
            addOrderedItem(mPendingJobs, jobStatus, mEnqueueTimeComparator);
            maybeRunPendingJobsLocked();
        }
    }
    return JobScheduler.RESULT_SUCCESS;
}