Progress bar in notification bar when uploading image?

前端 未结 6 1994
借酒劲吻你
借酒劲吻你 2020-12-05 02:07

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 entr

6条回答
  •  心在旅途
    2020-12-05 02:29

    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

提交回复
热议问题