Ongoing notification on Android honeycomb has inconsistent behavior
I have an ongoing notification for downloading files in the background. I have been successful in creating an multiple simultaneous updating progress bar notifications that are also able to be canceled. This works fine on all tested devices except for some of the more recent Android tablets with Honeycomb.
The effect is now that the original notification message is constantly redisplayed, preventing the user from clicking on the clock to bring up the list of ongoing notifications. As such, no progress bars are even seen. Has anyone been successful in essentially creating progress bar notifications on Honeycomb?
As a side, I also find that my black notification text is no longer readable with the black background of the notification list. Is there a way to set white text for Honeycomb devices?
Note: This has been tested on the Optimus Pad L-06C running Android 3.0.1 and the Motorola Xoom
Below is a the notification creation
// 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.开发者_如何学PythonsetTextViewText(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);
And here is how I update the notification:
// 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);
To fix this problem you need to set the bitwise Notification.FLAG_ONLY_ALERT_ONCE on your ongoing notification. This will ensure that the user is only ever notified when the notification is first displayed. Following that, they will have to open the notification tray to view the status of the notification.
Notification notification = new Notification();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
I think I found the issue to this if anyone is still looking. The problem is when you create a new notification, the when is set to the current system time. If you take a look at: http://developer.android.com/reference/android/app/Notification.html#when you will read that a download operation when should be stamped at the start. If you don't (like I was doing) and you have multiple download notifications, they will continue to reorder themselves based on the when field.
The notification list does not have a black background on GB; only the status bar was changed to black. What device is causing the trouble? Have you tried this on the standard GB to make sure this is not a problem specific to that device instead of something that changed in the platform? Do you know if this device is even something that is compatible with the CDD and passed CTS?
To solve this (on 4.0.3,did not try on previous API levels) I had to keep a reference to my Notification
, update it every time something changed, and then send the same Notification
object to NotificationManager.notify()
.
Altough I was setting the flags
as specified in @twaddingtion's answer, and sending the same id
to the NotificationManager
my notification were getting messed up in the SystemBar
.
精彩评论