如何设置通知的时间和日期 [英] how can i set time and date for notification
问题描述
在通知管理器中,我们使用此方法设置触发通知的时间和日期
in the notification manager we use this method to set time and date to fire notification
.setWhen(System.currentTimeMillis());
但 System.currentTimeMillis()
返回设备上的当前时间,但我想添加指定的时间,如(周五 15:00)
but the System.currentTimeMillis()
get back the current time on device but i want to add specified time like ( Friday 15:00 )
我该怎么做??我如何以毫秒为单位设置这个时间并在 .setWhen
how can i do that ?? how can i set this time in milliseconds and load it up in .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();
推荐答案
你应该使用 Java Calendar 类来指定日期.使用 getInstance()
工厂方法实例化一个 Calendar
对象.默认情况下,实例将使用您机器的默认时区的当前日期和时间进行初始化.Calendar
包含不同的 set()
方法来指定日期,选择一个更适合您需要的方法并使用它来指定您需要的日期.然后调用 getTimeInMillis()
方法,该方法将以毫秒为单位返回指定的日期.希望这会有所帮助.
You should use the Java Calendar class to specify a date. Instantiate a Calendar
object using the getInstance()
factory method. By default, the instance will be initialized with the current date and time for the default time zone of your machine. Calendar
contains different set()
methods to specify a date, choose one that's more appropriate for your needs and use it to specify the date you need. Then call the getTimeInMillis()
method, which will return specified date in milliseconds. Hope this helps.
这篇关于如何设置通知的时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!