我需要每天凌晨3点发出通知。它工作正常,并在凌晨3点左右发送通知。但是,当我在手机中测试时,通知会立即显示。我读过,如果时间已经过去,它会显示通知。我很好。 问题:当我点击通知时,它会正确打开我的活动。但几秒钟后,它正在加载另一个通知。如果我再次点击它,它将关闭通知并在几秒钟后创建一个新通知。
这是代码: 1.在onCreate()
活动中Calendar calendar = Calendar.getInstance();
// calendar.set(Calendar.MONTH, 10);
// calendar.set(Calendar.YEAR, 2014);
// calendar.set(Calendar.DAY_OF_MONTH, 13);
calendar.set(Calendar.HOUR_OF_DAY, 3);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
Intent myIntent = new Intent(LitCalActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(LitCalActivity.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
在我的服务类
中public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Code to notification setup
Intent litrcalintent = new Intent(this, LitCalActivity.class);
Bundle bundle = new Bundle();
bundle.putString("FromNotify", "YES");
litrcalintent.putExtras(bundle);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
litrcalintent, PendingIntent.FLAG_UPDATE_CURRENT);
SimpleDateFormat format = new SimpleDateFormat("MMMM dd yyyy");
String today = format.format(currentDate);
String todayDetails = getLitCalDetails();
Notification notification = new Notification.Builder(this)
.setContentTitle("Liturgical Calendar for " + today)
.setContentText(todayDetails)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setOngoing(false)
.addAction(R.drawable.ic_launcher, "more...", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}