单击时如何清除android上的通知

时间:2012-08-31 08:11:37

标签: android android-notifications android-notification-bar

每次点击通知时,屏幕上会弹出活动,我曾用它在状态窗口中触发通知。

我想在用户点击时清除通知,但每次都会打开活动。

我尝试不仅在通知上使用标志,还在PendingIntent和Intents上使用标志。

我的代码:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification not = new Notification(R.drawable.ic_launcher,
        "this is not important", System.currentTimeMillis());

Context context = MainActivity.this;

CharSequence title = "You have been notified";
CharSequence  details = "contiue with work";

not.flags =  Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, PendingIntent. FLAG_ONE_SHOT);

not.setLatestEventInfo(context, title, details, pending);
nm.notify(0, not);

还有:

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, Notification.FLAG_AUTO_CANCEL);

每次点击通知活动都会出现在屏幕上,我只想清除通知。

这是Logcat

09-01 00:21:10.910: E/AndroidRuntime(330): FATAL EXCEPTION: main
09-01 00:21:10.910: E/AndroidRuntime(330): java.lang.IllegalArgumentException: contentIntent required: pkg=nipun.notify id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Parcel.readException(Parcel.java:1326)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Parcel.readException(Parcel.java:1276)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.NotificationManager.notify(NotificationManager.java:111)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.NotificationManager.notify(NotificationManager.java:91)
09-01 00:21:10.910: E/AndroidRuntime(330):  at nipun.notify.MainActivity$1.onClick(MainActivity.java:49)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.view.View.performClick(View.java:2485)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.view.View$PerformClick.run(View.java:9080)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Handler.handleCallback(Handler.java:587)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
09-01 00:21:10.910: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-01 00:21:10.910: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 00:21:10.910: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
09-01 00:21:10.910: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 00:21:10.910: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 00:21:10.910: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您不应设置待定意图

删除以下行

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, PendingIntent. FLAG_ONE_SHOT);

并将null发送到

not.setLatestEventInfo(context, title, details, null);