Android Official Development Document Training Series Chinese version: Notifies the user of the progress shown in the notification

Source: Internet
Author: User

Original address: Http://android.xsoftlab.net/training/notify-user/display-progress.html#FixedProgress

A progress indicator is included in the notification to show the user a working state in progress. If you can make sure that the task takes a long time and you can tell at any time how much work it has done, you can use an indicator that determines the style (a progress bar). If you cannot determine how long the task will take, you can use an indicator of the indeterminate style (an active indicator).

Progress indicators are implemented by the ProgressBar class.

Using the progress indicator, you can call the Setprogress () method. Determining styles and indeterminate styles are discussed in the following sections.

Show OK Progress indicator

To display a progress indicator, call the setprogress (max, progress, False) method to add the indicator to the notification, and then publish the notification. The third parameter of the method is used to indicate whether the progress bar is a deterministic progress bar (true) or an indeterminate progress bar (false). As the operation is processed, the progress progress will increase, and the notification needs to be updated. At the end of the operation, progress should be equal to max. A general way is to set Max to 100 and then increment the progress as a percentage.

You can also choose to suppress the progress bar or remove the notification when the task is completed. In the previous case, remember to update the text of the notification to tell the user that the task is complete. In the latter case, call setprogress (0, 0, false) to complete the notification removal.

intID =1;... Mnotifymanager = (Notificationmanager) getsystemservice (context.notification_service); MBuilder =NewNotificationcompat.builder ( This); Mbuilder.setcontenttitle ("Picture Download"). Setcontenttext ("Download in Progress"). Setsmallicon (r.drawable.ic_notification);//Start a lengthy operation in a background threadNewThread (NewRunnable () {@Override         Public void Run() {intincr//Do the "lengthy" Operation             for(incr =0; INCR <= -; incr+=5) {//sets the progress indicator to a max value, the                    //Current completion percentage, and "Determinate"                    // StateMbuilder.setprogress ( -, INCR,false);//Displays the progress bar for the first time.Mnotifymanager.notify (ID, mbuilder.build ());//Sleeps the thread, simulating an operation                        //That takes time                        Try{//Sleep for 5 secondsThread.Sleep (5* +); }Catch(Interruptedexception e) {LOG.D (TAG,"Sleep Failure"); }            }//When the loop is finished, updates the notificationMbuilder.setcontenttext ("Download Complete")//Removes the progress bar. setprogress (0,0,false);        Mnotifymanager.notify (ID, mbuilder.build ()); }    }//Starts the thread by calling the run () method in its Runnable). Start ();

The final effect is as follows:

The diagram on the left shows the notifications in progress, and the diagram on the right shows the notifications after the task is completed.

Show indicator of continuous activity

To display an indicator of uncertainty, you need to call the setprogress (0, 0, True) method to display the progress bar in the notification, and then publish the notification. The first second parameter is ignored, and the third parameter determines whether the progress bar is an indeterminate progress bar. The final display has the same display style as the regular progress bar, except that it has been moving.

Publish the notification before the action starts, and the progress animation will continue to run until you modify the notification. When the operation is complete, call the setprogress (0, 0, False) method and update the notification to remove the activity indicator. Otherwise, the animation will not stop even after the task has been completed. So remember to change the notification text after the task is complete in order to tell the user that the action is complete.

// Sets the progress indicator to a max value, the current completion// percentage, and "determinate" statemBuilder.setProgress(100false);// Issues the notificationmNotifyManager.notify(id, mBuilder.build());

Locate the previous code and replace the following section. Remember that the third argument to the Setprogress () method is true:

// Sets an activity indicator for an operation of indeterminate lengthmBuilder.setProgress(00true);// Issues the notificationmNotifyManager.notify(id, mBuilder.build());

The final results are as follows:

Android Official Development Document Training Series Chinese version: Notifies the user of the progress shown in the notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.