Show ProgressBar in Notification Area like Google does when downloading from Android market
When you download from the Market you get informed about the progress in the notification area.
That's a really nice feature, which you may want for your own download or processing progress. I will show you the basics of generating such a Notification. All you need to do is add it to your code.
I included a screenshot. You can find the code at github.
First of all, you need your Notification object:
- // configure the notification
- final Notification notification = new Notification(R.drawable.icon, "simulating a download", System
- .currentTimeMillis());
- notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
- notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
- notification.contentIntent = pendingIntent;
- notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
- notification.contentView.setTextViewText(R.id.status_text, "simulation in progress");
- notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
then add it to the NotificationManager
- final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
- getApplicationContext().NOTIFICATION_SERVICE);
- notificationManager.notify(42, notification);
in your Thread call the NotificationManager regularly, after you updated your notification with new values:
- notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
- // inform the progress bar of updates in progress
- notificationManager.notify(42, notification);
The XML and the full example, ready to run, can be found on github.
- Login to post comments
Comments
Kontantinos Polychronis - ProgressBar in Notification Area & (not verified) - Wed, 10/27/2010 - 12:02
[...] this tutorial by NICO HEID from [...]
Umar (not verified) - Fri, 12/17/2010 - 16:10
such a nice tutorial.
do you have any uploading related tutorial?
for example
showing progress while uploading?
Techflag (not verified) - Sat, 07/30/2011 - 05:38
hi,did you get the demo about "Show ProgressBar in Notification Area like Google does when downloading from Android market ",if you have the demo,would you send one to me!
thanks a lot!
Anonymous (not verified) - Tue, 09/13/2011 - 10:50
Can you suggest me that how to add the percentage of downloading done in the ProgressBar.... Help will be greatly appreciated.
Nico Heid - Wed, 09/14/2011 - 20:25
just add a text view to the layout https://github.com/nheid/unitedcoders-android/blob/master/res/layout/dow... and write the progress into it.
Sundar (not verified) - Thu, 04/19/2012 - 11:14
I want to show the progress bar in notification area while uploading the video and i want to remove the progress bar after the video has been uploaded.could u help me in such a way?
Alik (not verified) - Mon, 12/05/2011 - 08:20
Great example.
The USB notification text and icon are much more darker.
How can I set the same look and feel?
Thanks.
butelo (not verified) - Tue, 03/20/2012 - 17:03
Very nice tx
How can I make the progress bar dissapear when the task is done?
nico (not verified) - Sat, 04/21/2012 - 17:34
you should find your answer here: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int