Conditions for JobInfo.Builder for showing Notifications not working

Viewed 30

I am a newbie in android development and currently developing a Reminder appfor showing the notifications every 15 mins, when charging and connected to wifi . Although the notification is showing every 15 mins. But the conditions for showing notification only when charging and when connected to wifi are not working. So it shows notifications even when not charging or not connected to wifi. I have used the below code for scheduling it.

public class ReminderUtilities {

private static final int REMINDER_INTERVAL_TIME    = 15;
private static final int REMINDER_INTERVAL_MILLIS = (int) (TimeUnit.MINUTES.toMillis(REMINDER_INTERVAL_TIME));

private static boolean mInitialized ;


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
synchronized public static void scheduleTaskReminder(Context context) {

    if( mInitialized ) {
        return;
    }

    ComponentName serviceComponent = new ComponentName(context, WaterReminderJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent)
                                         .setPeriodic(REMINDER_INTERVAL_MILLIS)
                                         .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                                         .setRequiresCharging(true);

    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    scheduler.schedule(builder.build());

    mInitialized = true;
}

}

1 Answers

The setPeriodic(long millis) method by JobInfo.Builder internally uses the setOverrideDeadline(long millis) method.

And when the override is set, then even though conditions are not met, if the deadline is crossed then the job will execute. Therefore, for each interval, your app waits for the conditions to be met but if they are not met even at the end, the job is started.

This is stated in the official docs here

Related