嗨,我是新的Android .. 我已经开发了一个程序来设置特定时间的通知 但问题是它只显示2011年10月之前的日期通知 如果我输入任何十一月的日期,它没有显示任何通知.. 看起来很奇怪但真的卡在这里....
主要活动..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar cal = Calendar.getInstance(); // for using this you need to
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.DAY_OF_MONTH, 25);
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 28);
Intent alarmintent = new Intent(getApplicationContext(),
AlarmReceiver.class);
alarmintent.putExtra("title", "Title of our Notification");
alarmintent.putExtra("note", "Description of our Notification");
int HELLO_ID = 1;
PendingIntent sender = PendingIntent.getBroadcast(
getApplicationContext(), HELLO_ID, alarmintent,
PendingIntent.FLAG_UPDATE_CURRENT |
Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
和警报接收器活动......
public class AlarmReceiver extends BroadcastReceiver {
private static final String NotificationManager = null;
private static int NOTIFICATION_ID = 0;
@Override
public void onReceive(Context context, Intent intent) {
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Combi Note", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID, new Intent(context,
AlarmReceiver.class), 0);
Bundle extras = intent.getExtras();
String title = extras.getString("title");
String note = extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID, notification);
}
};
答案 0 :(得分:1)
Calendar.set(Calendar.MONTH, month)
预计基于0的值。 11月是12月。
答案 1 :(得分:0)
您正在为2011年5月5日设置闹钟,此日期已过,因此会立即执行。
当您收到Calendar
的实例时,我认为系统会自动填写当前日期&时间。因此,只需更改 您需要更改的内容(如果您想在5天内发送通知,请在日历实例中更改 日期)。
顺便说一下,当点击通知时,它会再次呼叫您的BroadcastReceiver
,这次意图不会拥有您在活动中添加的额外内容。单击通知时,您应启动活动 - 而不是广播接收器。