0

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..

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Sid
  • 2,792
  • 9
  • 55
  • 111
  • did u try for alarm manager ? – Quick learner Apr 11 '18 at 09:13
  • https://stackoverflow.com/questions/24196890/android-schedule-task-to-execute-at-specific-time-daily?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Quick learner Apr 11 '18 at 09:14
  • Yes I tried with alarm manager. But it has limitations, if mobile gets switched off it dose not trigger the alarm also it dose not work with red mi , if the app is in ram then only alarm gets triggered. It dose not permit auto permissions in red mi OS. So I thought to switch to some other scheduler. @quicklearner – Sid Apr 11 '18 at 09:25
  • well u have to restart it when phone restarts its possible – Quick learner Apr 11 '18 at 13:32
  • but dose not work with red mi devices. this Android job also dose not work with red mi. Which scheduler to be used? Also I observed alarms are missed some times in other devices too. @quicklearner – Sid Apr 12 '18 at 05:31
  • well in redmi devices there is an option called Autostart , user has to select it manually for each app – Quick learner Apr 12 '18 at 05:40
  • is the problem due to app gets killed when user do that? – Quick learner Apr 12 '18 at 05:41
  • In red mi note 4 I was not getting the option to auto start the permissions. and in other devices may be when app is not in use the alarm dose not work sometimes. @quicklearner – Sid Apr 12 '18 at 06:06
  • for redmi note 4 go to persmission then autostart select app – Quick learner Apr 12 '18 at 06:52
  • Would you like to have a quick check of my answer? @Sid – exploitr Sep 21 '18 at 03:51

1 Answers1

1

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.

exploitr
  • 843
  • 1
  • 14
  • 27
  • is it possible to shedule in exact time eg. 23:59:59 ? – Mahen Oct 05 '18 at 18:53
  • But would this run even when the app is not open? Also, the user has to open the app to schedule the job right? Is there a way to do it even when the user does not open the app? Say the user installs the app and never opens it but I want to send the user daily notifications. – Anirudh Ganesh Jul 21 '20 at 17:58
  • @AnirudhGanesh This answer is very old, it used to work well back then. Also, this library is outdated now. We have work-manager now which executes Jobs with gurrantee*. https://developer.android.com/topic/libraries/architecture/workmanager/basics – exploitr Jul 21 '20 at 20:13
  • It's not deprecated atleast!, I'm able to use it. I'm also using Work manager to build periodic requests and one time requests but then again, for running a work at an exact time, google still recommends alarm manager and jobs API. Funny thing about jobs Api is that, when the device goes into doze mode, I think it doesn't run! I tried it with different devices and for older API's it's working smoothly but in the newer API's have to resort to something else. – Anirudh Ganesh Jul 21 '20 at 20:27
  • Yeah, google cares a lot about battery now. Even some 3rd party OEMs break normal features to squeeze the least amount of battery now. https://dontkillmyapp.com/ – exploitr Jul 22 '20 at 06:04