显示特定日期和时间的通知 [英] show a notification on a particular date and time

查看:77
本文介绍了显示特定日期和时间的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示特定日期和时间的通知(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);

希望这对您有所帮助。

这篇关于显示特定日期和时间的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆