JobScheduler Lint warning for schedule method

时间:2018-03-25 19:12:04

标签: android job-scheduling android-jobscheduler

Android Studio shows me a Nullpointer Lint warning for the schedule method. Can I ignore this or should I take any precautions?

public void scheduleJob(View v) {
    ComponentName componentName = new ComponentName(this, ExampleJobService.class);
    JobInfo info = new JobInfo.Builder(123, componentName)
            .setRequiresCharging(true)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setPersisted(true)
            .setPeriodic(15 * 60 * 1000)
            .build();

    JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    int resultCode = scheduler.schedule(info);
    if (resultCode == JobScheduler.RESULT_SUCCESS) {
        Log.d(TAG, "Job scheduled");
    } else {
        Log.d(TAG, "Job scheduling failed");
    }
}

1 个答案:

答案 0 :(得分:1)

According to https://developer.android.com/reference/android/app/job/JobScheduler.html, JobScheduler was added in API 21.
So if you try to use it below API 21, it will be null, and your app will crash.

If your minimum API is 21 or above, you can ignore the warning.