我正在尝试安排每天晚上10点开始的工作。
我尝试使用setExact方法并通过将22小时转换为毫秒来提供毫秒数,并且为了测试我执行应用程序并将系统时间更改为晚上10点但作业未执行。
我还尝试将即将到来的时间转换为12:45转换为毫秒并给予setExact方法。但这也行不通。
如何设置和测试?
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;
}
}
}
此外,我已安排了一份定期工作,这项工作不适用于某些设备,例如红色mi,在一台三星设备上没有重复,但在moto g4上它运行良好。
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();
}
}
有人可以帮忙吗?谢谢..
答案 0 :(得分:1)
您必须使用DailyJob
代替Job
在每天的特定时间执行某些操作。
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;
}
}
另外:如果你想设置特定的分钟和秒数,那么使用(假设在15:48:05和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));
希望这有帮助。