作为初学者,我写了一个通知代码。我将该代码放在MainActivity中。 现在的问题是,我的应用程序在凌晨8点显示通知。但在其他时间,如果我重新启动我的应用程序,它会再次显示通知(在任何其他时间)。基本上我没有取消它出现的通知。虽然我把AutoCancel设为true。我错过了什么吗?
MainActivity中的代码,onCreateMethod:
calendar = Calendar.getInstance();
PendingIntent pendingIntent ;
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(MainActivity.this, NotificationBroadcasrReceiver.class);
intent.putExtra("NotificationType","GoodMorning");
pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), (int)System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getBaseContext()
.getSystemService(getBaseContext().ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
在此之后,它来到BroadCast Receiver:
onReceive中的代码是
Intent intentNew = new Intent(context, CustomIntentService.class);
intentNew.putExtra("NotificationType",intent.getStringExtra("NotificationType"));
context.startService(intentNew);
现在,自定义意图servcice类具有以下代码..
Intent notificationIntent = new Intent(this,CustomActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.happiness_icon);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentTitle("Positive Minds");
mBuilder.setCustomBigContentView(remoteViews);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager = (NotificationManager)this
.getSystemService(this.NOTIFICATION_SERVICE);
mNotificationManager.notify((int)System.currentTimeMillis(), mBuilder.build());
答案 0 :(得分:0)
我使用下面提到的代码来解决这个问题。
if(calendar1.getTime().compareTo(new Date()) < 0)
calendar1.add(Calendar.DAY_OF_MONTH, 1);