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");
}
}
答案 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.