Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

android - Progress bar in notification bar when uploading image?

I'd like my app to upload an image to a web server. That part works.

I'm wondering if it's possible to somehow show the progress of the upload by entering an entry in the "notification bar". I see the Facebook app does this.

When you take a picture and choose to upload, the app lets you continue on, and somehow puts the picture upload notifications in a progress bar in the notification bar. I think that's pretty slick. I guess they spawn a new service or something to handle the upload and update that progress bar in the notification bar every so often.

Thanks for any ideas

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Android, in order to display a progress bar in a Notification, you just need to initialize setProgress(...) into the Notification.Builder.

Note that, in your case, you would probably want to use even the setOngoing(true) flag.

Integer notificationID = 100;

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//Set notification information:
Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext());
notificationBuilder.setOngoing(true)
                   .setContentTitle("Notification Content Title")
                   .setContentText("Notification Content Text")
                   .setProgress(100, 0, false);

//Send the notification:
Notification notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);

Then, your Service will have to notify the progress. Assuming that you store your (percentage) progress into an Integer called progress (e.g. progress = 10):

//Update notification information:
notificationBuilder.setProgress(100, progress, false);

//Send the notification:
notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);

You can find more information on the API Notifications page: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Progress


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...