显示特定日期和时间的通知

编程入门 行业动态 更新时间:2024-10-24 14:20:44
本文介绍了显示特定日期和时间的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要显示特定日期和时间的通知(2015年6月27日13:00)。最初,我正在显示吐司消息。我已经创建了一个广播接收器来做到这一点。我这样称呼待处理的意图-

I need to show a notification on a particular date and time (27-06-2015 13:00). Initially I am showing a toast message. I have created a broadcast receiver to do that. I am calling the pending intent like this -

活动代码-

Calendar cal=Calendar.getInstance(); cal.set(Calendar.MONTH,(5)); cal.set(Calendar.YEAR,2015); cal.set(Calendar.DAY_OF_MONTH, 27); cal.set(Calendar.HOUR_OF_DAY,13); cal.set(Calendar.MINUTE,00); Intent myIntent1 = new Intent(this, AlarmBroadCustReciver.class); PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1253, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent1);

Broadcust接收方代码-

public class AlarmBroadCustReciver extends BroadcastReceiver { public AlarmBroadCustReciver() { } public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show(); } }

我正在尝试通过更改时间来检查我的电话。我将电话时间更改为12:59。但是,当我打电话给广播接收器时,按照我的逻辑,当时间为13:00时,它会立即显示吐司消息。我做错了什么吗?

I am trying to check that by changing the time of my phone.I am changing the phone time to 12:59. But When I am calling the Broadcast Receiver it instantly giving the toast message it should show when the time is 13:00 as per my logic. Am I doing anything wrong?

推荐答案

您的代码中的每件事看起来都很清楚,可能是 Calendar 您已初始化的obj。

Every thing looks clear in your code may be it is the problem of Calendar obj that you have initialized.

尝试如下操作,

Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.clear(); cal.set(2015,5,27,13,00);

并设置

alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent1);

希望这对您有所帮助。

更多推荐

显示特定日期和时间的通知

本文发布于:2023-11-27 08:36:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   通知   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!