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