Android通知旨在自行清除

Android通知旨在自行清除,android,notifications,android-intent,Android,Notifications,Android Intent,我已经阅读了许多关于如何创建通知消息的示例。 我想要实现的是,因为通知将由一个小部件执行,我希望 当用户单击时,单击以清除其自身的通知意图。我没有要返回的活动。 为了我的目的,通知只是简单地通知,没有别的。 那么,仅仅清除/取消自身的意图代码是什么呢。 下面的代码是由按钮启动的活动(不包括按钮代码)。通知将由后台服务触发 CharSequence title = "Hello"; CharSequence message = "Hello, Android!"; final Notificati

我已经阅读了许多关于如何创建通知消息的示例。 我想要实现的是,因为通知将由一个小部件执行,我希望 当用户单击时,单击以清除其自身的通知意图。我没有要返回的活动。 为了我的目的,通知只是简单地通知,没有别的。 那么,仅仅清除/取消自身的意图代码是什么呢。 下面的代码是由按钮启动的活动(不包括按钮代码)。通知将由后台服务触发

CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

谢谢

我能看到的唯一方法是让您的
通知
意图
指向后台
服务
。启动此服务时,它将使用
NotificationManager.cancel(int-id)
清除给定的
通知。
服务将自动停止。它并不漂亮,也不容易实现,但我找不到其他方法来实现它。

请查看

位按位或入标志字段,如果用户单击通知时应取消通知,则应设置该字段

编辑:

notification.flags |= Notification.FLAG_AUTO_CANCEL;

在notification.flags而不是notification.defaults中设置标志

例如:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

如果您使用的是
NotificationCompat.Builder
(android.support.v4
的一部分),那么只需调用其对象的方法
setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
有些人报告说,
setAutoCancel()
对他们不起作用,所以您也可以尝试这种方法

builder.build().flags |= Notification.FLAG_AUTO_CANCEL;

使用setContentIntent可以解决您的问题:

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
例如:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

通常,您可能希望将用户引导到相关内容,因此可能会将“new Intent()”替换为其他内容。

如果您希望通过代码取消通知,请在取消呼叫时使用
通知ID
。是的,我会通过按钮调用,但是当用户选择notification.Works-for-me解决方案时,我想从代码中调用它,但是有点混乱(需要编写附加服务)。但在服务中,我可以访问调用意图,在调用意图中可以给出通知ID作为参数。我的代码已经在使用FLAG\u AUTO\u CANCEL,但实际上通知不会在单击时消失。您的代码使用了错误的(放入默认字段),您需要在flags字段中按位或如下所示:
notification.flags |=notification.FLAG_AUTO_CANCEL谢谢通知.flags |=通知.FLAG_自动_取消;工作了,Flag好像不在这里工作。进一步设置此标志会导致连续播放通知声音在
notify()
之后立即取消通知会导致通知根本不显示。使用操作(使用addAction())似乎不会自动取消通知
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());