我在通知中添加了两个操作按钮,当我单击其中任何一个时,它们会执行所需的操作,但通知仍保留在我的通知抽屉中。我知道单击操作按钮时可以从通知抽屉中删除通知,因为这就是 Gmail 的功能。如果我单击主通知,它将打开应用程序并从通知抽屉中删除通知。
这是我的代码片段:
Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
completeIntent.putExtra("rowid", inrowid);
completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("text")
.setContentIntent(notificationReleaseIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);
编辑 -
正如 zionpi 指出的,我需要调用 notification.cancel();单击 addAction 按钮后删除通知。我只是将此方法添加到 PendingIntent 所指向的类中。
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
所以你想要删除你的通知,首先要有一个通知id。
然后调用类似下面的方法来消除它。
public static void CancelNotification(Context ctx, int notifyId) {
String s = Context.NOTIFICATION_SERVICE;
NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
mNM.cancel(notifyId);
}
您可能想参考this https://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar and this https://stackoverflow.com/questions/2839727/remove-the-notification-icon-from-the-status-bar或者发帖。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)