【发布时间】:2018-12-07 09:00:19
【问题描述】:
我有一个JobScheduler 的代码,在单击按钮后运行:
ComponentName serviceName = new ComponentName(v.getContext(), TestJobService.class);
JobInfo jobInfo = new JobInfo.Builder(1, serviceName)
.setRequiresDeviceIdle(false)
.setRequiresCharging(false)
.setPeriodic(900000)
.setBackoffCriteria(10000, JobInfo.BACKOFF_POLICY_LINEAR)
.setPersisted(true)
.setRequiresDeviceIdle(false)
.build();
JobScheduler scheduler = (JobScheduler) v.getContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
scheduler.schedule(jobInfo);
我在TestJobService extends JobService 有一个任务要显示Notification:
public boolean onStartJob(JobParameters params) {
Intent test = new Intent(this, Diarys.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("test text")
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logonotification)
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(test);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
mNotificationManager.notify(1000, notificationBuilder.build());
return false;
}
当我点击运行JobScheduler-code 的按钮时,Notification 会立即显示,而不是在 900000 毫秒(15 分钟)之后。
如何在点击按钮 15 分钟后运行作业?
【问题讨论】:
-
900000很难阅读,即使你数了数,也不明显它等于 15 分钟。我建议TimeUnit.MINUTES.toMillis(15)。
标签: java notifications android-jobscheduler