0

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

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

我尝试不仅在通知上使用标志,还在 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)
4

1 回答 1

0

您不应设置待处理的意图

删除下面的行

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

并将 null 发送到

not.setLatestEventInfo(context, title, details, null);
于 2012-08-31T08:15:42.437 回答