How to set the exact time to android job for a daily job at fixed time?

3.3k views Asked by At

I am trying to schedule the job which will run at 10 PM daily.

I tried to use the setExact method and provide the milliseconds by converting 22 hours to milliseconds and for testing I executed the app and changed the system time to 10 PM but the job did not execute.

I also tried to give coming time so converted 12:45 to milliseconds and given to setExact method. But that also did not work.

How can I set this and test?

FileTrackJob

class FileTrackJob extends Job {

        static final String TAG = "FileTracking";

        @NonNull
        @Override
        protected Result onRunJob(Params params) {

            PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0,
                    new Intent(getContext(), MainActivity.class), 0);

            Calendar cal = Calendar.getInstance();
            Date currentLocalTime = cal.getTime();
            DateFormat date = new SimpleDateFormat("HH:mm a");
    // you can get seconds by adding  "...:ss" to it
            String localTime = date.format(currentLocalTime);

            Notification notification = new NotificationCompat.Builder(getContext())
                    .setContentTitle("Android Job Demo")
                    .setContentText("Notification from Android Job Demo App. " + localTime)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setShowWhen(true)
                    .setColor(Color.RED)
                    .setLocalOnly(true)
                    .build();

            NotificationManagerCompat.from(getContext())
                    .notify(new Random().nextInt(), notification);


            return Result.SUCCESS;
        }

        static void scheduleNoti() {
            new JobRequest.Builder(TrackingJob.TAG)
                    //  .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(15))
                    .setExact(44820000)
                    .setUpdateCurrent(true)
                    .setPersisted(true)
                    .build()
                    .schedule();
        }
    }

MainActivity

FileTrackJob.scheduleNoti();

MainApp

public class MainApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        JobManager.create(this).addJobCreator(new DemoJobCreator());
    }
}

DemoJobCreator

class DemoJobCreator implements JobCreator {

    @Override
    public Job create(String tag) {
        switch (tag) {
            case TrackingJob.TAG:
                return new TrackingJob();

            case FileTrackJob.TAG:
                return new FileTrackJob();
            default:
                return null;
        }
    }
}

Also I have scheduled one periodic job, this job is not working on some devices like red mi,on one samsung device its not repeated but on moto g4 plus it worked well.

class TrackingJob extends Job {

    static final String TAG = "tracking";

    @NonNull
    @Override
    protected Result onRunJob(Params params) {

        Intent pi = new Intent(getContext(), GetLocationService.class);
        getContext().startService(pi);


        return Result.SUCCESS;
    }

    static void schedulePeriodic() {
        new JobRequest.Builder(TrackingJob.TAG)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(15))
                .setUpdateCurrent(true)
                .setPersisted(true)
                .build()
                .schedule();
    }
}

Can anyone help with this please? Thank you..

1

There are 1 answers

5
exploitr On

You must use DailyJob instead of Job to do something at the specific time daily.

public static final class MyDailyJob extends DailyJob {

    public static final String TAG = "MyDailyJob";

    public static void schedule() {
        // schedule between 1 and 6 *PM*
        DailyJob.schedule(new JobRequest.Builder(TAG), TimeUnit.HOURS.toMillis(13), TimeUnit.HOURS.toMillis(18));
    }

    @NonNull
    @Override
    protected DailyJobResult onRunDailyJob(Params params) {
        return DailyJobResult.SUCCESS;
    }
}

Plus: If you want to set specific minutes and seconds, then use (assume between 15:48:05 and 17:42:09) :

    DailyJob.schedule(new JobRequest.Builder(TAG), TimeUnit.HOURS.toMillis(15)+TimeUnit.MINUTES.toMillis(48)+TimeUnit.SECONDS.toMillis(5), 
TimeUnit.HOURS.toMillis(17)+TimeUnit.MINUTES.toMillis(42)+TimeUnit.SECONDS.toMillis(9));

Hope this helps.