Android通知意图自行清除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3479428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Notification intent to clear it self
提问by John
I have read many examples of how to create notification messages. What i wanted to achieve, is because the notification will be executed by a widget, i would like the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to. The notification for my purposes will just plainly notify, nothing else. So what would be the code of an intent that just clear/cancel itself. The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
我已经阅读了许多有关如何创建通知消息的示例。我想要实现的是因为通知将由小部件执行,我希望在用户单击时单击通知意图以清除它。我没有要返回的活动。出于我的目的的通知只会简单地通知,没有别的。那么,只是清除/取消自身的意图的代码是什么。下面的代码是由按钮启动的活动(不包括按钮代码),通知将由后台服务启动。
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);
Thanks
谢谢
回答by Pentium10
Check out FLAG_AUTO_CANCEL
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
如果在用户单击通知时应取消通知,则将按位或运算到标志字段中的位应设置该位。
EDIT :
编辑 :
notification.flags |= Notification.FLAG_AUTO_CANCEL;
回答by Proxy32
Set the flags in notification.flags instead of notification.defaults.
在notification.flags 中设置标志而不是notification.defaults。
Example:
例子:
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
回答by sandalone
If you're using NotificationCompat.Builder
(a part of android.support.v4
) then simply call its object's method setAutoCancel
如果您正在使用NotificationCompat.Builder
(的一部分android.support.v4
),则只需调用其对象的方法setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel()
did not work for them, so you may try this way as well
有些人报告说这setAutoCancel()
对他们不起作用,所以你也可以试试这种方式
builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
回答by Amal Dev S I
Using setContentIntent should solve your problem:
使用 setContentIntent 应该可以解决您的问题:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
For example:
例如:
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());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.
通常,您可能希望将用户定向到相关内容,因此可能会将“new Intent()”替换为其他内容。
回答by swathi
/**
Post a notification to be shown in the status bar.
Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
*/
notificationManager.notify(NOTIFICATION_ID, notification);
/**
Cancel a previously shown notification given the notification id you've saved before
*/
notificationmanager.cancel(NOTIFICATION_ID);
回答by iandisme
The only way I can see of doing this is to have your Notification
's Intent
point to a background Service
. When this Service is launched, it would clear the given Notification
using NotificationManager.cancel(int id)
. The Service
would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.
我认为这样做的唯一方法是将您Notification
的Intent
观点指向背景Service
。当此服务启动时,它将Notification
使用清除给定的NotificationManager.cancel(int id)
。然后Service
会自行停止。它不漂亮,也不容易实现,但我找不到任何其他方法来做到这一点。