如何设置通知的时间和日期

eng*_*med 8 notifications android

在通知管理器中,我们使用此方法设置触发通知的时间和日期

.setWhen(System.currentTimeMillis());
Run Code Online (Sandbox Code Playgroud)

但是System.currentTimeMillis()在设备上取回当前时间但我想添加指定的时间(星期五15:00)

我怎样才能做到这一点 ??我该如何设置这个时间(以毫秒为单位)并加载它.setWhen

提前致谢

这个代码我用

Notification.Builder builder =
new Notification.Builder(MyActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setTicker(“Notification”)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE)
.setSound(
RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 0, 1);
Notification notification = builder.getNotification();
Run Code Online (Sandbox Code Playgroud)

Ego*_*gor 8

您应该使用Java Calendar类来指定日期.Calendar使用getInstance()工厂方法实例化对象.默认情况下,将使用计算机默认时区的当前日期和时间初始化实例.Calendar包含set()指定日期的不同方法,选择一个更适合您需求的方法,并使用它来指定您需要的日期.然后调用该getTimeInMillis()方法,该方法将以毫秒为单位返回指定的日期.希望这可以帮助.