Android 4:无法通过刷卡解除通知

Jam*_*mes 10 android android-notifications

我有一些代码可以创建一些通知,这是非常基本的.

int icon = R.drawable.notification;
CharSequence tickerText = "Text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "Text";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, RequestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

mNotificationManager.notify(notificationID, notification);
Run Code Online (Sandbox Code Playgroud)

它在2.1中都运行良好.在4.0中,除了滑动到解除操作不起作用外,一切正常.通知略微偏向一侧,然后反弹并反弹.任何的想法?谢谢.

And*_*din 13

您无法刷掉通知,因为它处于"ONGOING"状态.

首先是解决方案:

用以下代码替换设置标志:

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)

默认值用于defaults-section,flags-section的标志.

现在它正在进行的原因是什么?

您可能已经知道通知的标志(和默认值)是通过按位操作设置的.意味着每个标志具有一个常数值,该值是2的幂.添加它们会产生一组标志的唯一编号,这使得计算实际设置哪些标志的速度非常快.

Notification.DEFAULT_VIBRATENotification.FLAG_ONGOING_EVENT 具有相同的2的含有值.


Int*_*iya 6

你应该使用setOngoing(布尔持续)

设置这是否是"正在进行的"通知.正在进行的通知不能被用户解雇,因此您的应用程序或服务必须负责取消它们.它们通常用于指示用户主动参与的后台任务(例如,播放音乐)或以某种方式待决并因此占用设备(例如,文件下载,同步操作,活动网络连接).

您可以使用

.setOngoing(false);
Run Code Online (Sandbox Code Playgroud)