Android蜂窝上正在进行的通知具有不一致的行为

时间:2011-06-06 03:17:01

标签: android notifications progress-bar android-3.0-honeycomb

我持续通知您在后台下载文件。我已经成功创建了多个同时更新进度条通知,这些通知也可以取消。这适用于所有经过测试的设备,除了一些最近使用Honeycomb的Android平板电脑。

现在效果是原始通知消息不断重新显示,阻止用户点击时钟以显示正在进行的通知列表。因此,甚至没有看到进度条。有没有人成功地在Honeycomb上创建进度条通知?

作为一方,我还发现我的黑色通知文本不再具有通知列表的黑色背景。有没有办法为Honeycomb设备设置白色文本?

注意:这已经在运行Android 3.0.1和Motorola Xoom的Optimus Pad L-06C上进行了测试

以下是通知创建

// Create new notification for downloading
mNotification = new Notification(R.drawable.owl_icon, getNotificationText(R.string.notification_content_downloading), 0);
mNotification.flags |= (Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT);

// Create custom progress bar view
RemoteViews contentView = new RemoteViews(CourseSyncService.this.getPackageName(), R.layout.notification_downloading);
contentView.setTextViewText(R.id.notificationTitle, mCourseTitle);
contentView.setProgressBar(R.id.notificationProgressBar, 100, 0, false);
contentView.setTextViewText(R.id.notificationPercentage, "0%");
mNotification.contentView = contentView;

// Create pending intent for the notification
Intent notificationIntent = new Intent(CourseSyncService.this, CancelDownloadActivity.class);
notificationIntent.putExtra(CourseSyncService.KEY_USER_ID, mUserId);
notificationIntent.putExtra(CourseSyncService.KEY_COURSE_ID, mCourseId);
notificationIntent.putExtra(CourseSyncService.KEY_COURSE_TITLE, mCourseTitle);
PendingIntent contentIntent = PendingIntent.getActivity(CourseSyncService.this, mCourseId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.contentIntent = contentIntent;

// Launch notification
mNotificationManager.notify(mCourseId, mNotification);

以下是我更新通知的方式:

// Update the progress bar of the notification view 
mNotification.contentView.setProgressBar(R.id.notificationProgressBar, mItemCount, mProgressCount, false);
mNotification.contentView.setTextViewText(R.id.notificationPercentage, String.valueOf(mProgress) + "%");
mNotificationManager.notify(mCourseId, mNotification);

4 个答案:

答案 0 :(得分:7)

要解决此问题,您需要在正在进行的通知中设置按位Notification.FLAG_ONLY_ALERT_ONCE。这将确保仅在首次显示通知时通知用户。之后,他们必须打开通知托盘才能查看通知的状态。

Notification notification = new Notification();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

答案 1 :(得分:1)

如果有人还在寻找,我想我发现了这个问题。问题是当您创建新通知时,when设置为当前系统时间。如果您查看:http://developer.android.com/reference/android/app/Notification.html#when,您将在开始时标记下载操作。如果你没有(就像我一样)并且你有多个下载通知,他们将继续根据when字段重新排序。

答案 2 :(得分:0)

通知列表在GB上没有黑色背景;只有状态栏被更改为黑色。什么设备造成了麻烦?您是否在标准GB上尝试过此操作以确保这不是特定于该设备的问题而不是平台中发生变化的问题?你知道这个设备是否与CDD兼容并通过了CTS吗?

答案 3 :(得分:0)

要解决此问题(在4.0.3上,没有尝试以前的API级别)我必须保留对Notification的引用,每次更改时都更新它,然后发送相同的{{1} } object to Notification

Altough我按照@ twaddingtion的回答中的说明设置了NotificationManager.notify(),并将相同的flags发送到id,我的通知在NotificationManager中搞砸了。