如何在用户点击时关闭通知

时间:2016-04-23 15:25:44

标签: android notifications

用户在通知栏中点击通知后,我无法关闭通知。我知道setAutoCancel方法,但它似乎不起作用。不过这是我的代码。

MondayNotificationService.class

@Override
public void onReceive(Context context, Intent intent) {
    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.icon);
        builder.setContentTitle(context.getString(R.string.app_name));

    if (intent.getAction().equalsIgnoreCase("FirstPeriod")) {
        builder.setContentText("Microwave Engineering");
        builder.setContentIntent(PendingIntent.getActivity(context, 100, new Intent(context, Monday.class), 0));
        Notification notification = builder.build();
        builder.setAutoCancel(true);
        int notificationID = 100;
        notificationManager.notify(notificationID, notification);

        Handler handler = new Handler();
        long delayInMilliseconds = 300000;
        handler.postDelayed(new Runnable() {
            public void run() {
                notificationManager.cancel(100);
            }
        }, delayInMilliseconds);

    } if (intent.getAction().equalsIgnoreCase("SecondPeriod")) {
        builder.setContentText("Digital Signal Processing");
        builder.setContentIntent(PendingIntent.getActivity(context, 101, new Intent(context, Monday.class), 0));
        Notification notification = builder.build();
        int notificationID = 101;
        builder.setAutoCancel(true);
        notificationManager.notify(notificationID, notification);

        Handler handler = new Handler();
        long delayInMilliseconds = 300000;
        handler.postDelayed(new Runnable() {
            public void run() {
                notificationManager.cancel(101);
            }
        }, delayInMilliseconds);
    }

Monday.class

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_monday);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    Intent firstIntent = new Intent(this, MondayNotificationService.class);
    firstIntent.setAction("FirstPeriod");
    PendingIntent firstAlarmIntent = PendingIntent.getBroadcast(this, 100, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    calendar.set(2016, 3, 23, 20, 40, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), firstAlarmIntent);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, firstAlarmIntent);

    Intent secondIntent = new Intent(this, MondayNotificationService.class);
    secondIntent.setAction("SecondPeriod");
    PendingIntent secondAlarmIntent = PendingIntent.getBroadcast(this, 101, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    calendar.set(2016, 3, 23, 20, 12, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), secondAlarmIntent);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, secondAlarmIntent);

当通知在5分钟后过期时,我打开应用程序并点击"星期一"按钮然后所有通知立即显示在通知栏中(即使是过期的通知)。 任何有助于理解出错的帮助都将深受赞赏。

2 个答案:

答案 0 :(得分:3)

正如Mike M.所指出的,您需要在调用Builder之前配置build()

你有:

    Notification notification = builder.build();
    builder.setAutoCancel(true);

反转这些行:

    builder.setAutoCancel(true);
    Notification notification = builder.build();

答案 1 :(得分:0)

notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

根据文件

  

如果在用户点击通知时应取消通知,则应将位置按位进入应设置的标志字段

相关问题