【发布时间】:2016-07-21 07:05:10
【问题描述】:
我正在使用警报在名为 Mainmenu 的活动上创建通知,这里是代码
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
收到通知后,点击通知我必须打开相同的Mainmenu 活动。
问题:再次生成一个通知,再次单击通知主菜单活动将打开并继续
这是通知代码
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;
【问题讨论】:
-
你考虑过使用
IntentService吗?它会在工作完成后自行完成,然后在再次触发之前不会再次调用。 -
你用过我的解决方案吗?
-
是的,谢谢
标签: android notifications android-service alarmmanager android-pendingintent