| && repo sync -j8 | 0aae000 | 2012-10-04 16:34:23 -0700 | [diff] [blame] | 1 | page.title=Displaying Progress in a Notification |
| 2 | parent.title=Notifying the User |
| 3 | parent.link=index.html |
| 4 | |
| 5 | trainingnavtop=true |
| 6 | previous.title=Using Expanded Notification Styles |
| 7 | previous.link=expanded.html |
| 8 | |
| 9 | @jd:body |
| 10 | |
| 11 | <div id="tb-wrapper"> |
| 12 | <div id="tb"> |
| 13 | |
| 14 | <!-- table of contents --> |
| 15 | <h2>This lesson teaches you to</h2> |
| 16 | <ol> |
| 17 | <li><a href="#FixedProgress">Display a Fixed-duration progress Indicator</a></li> |
| 18 | <li><a href="#ActivityIndicator">Display a Continuing Activity Indicator</a></li> |
| 19 | </ol> |
| 20 | |
| 21 | <!-- other docs (NOT javadocs) --> |
| 22 | <h2>You should also read</h2> |
| 23 | |
| 24 | <ul> |
| 25 | <li> |
| 26 | <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Notifications</a> API Guide |
| 27 | </li> |
| 28 | <li> |
| 29 | <a href="{@docRoot}guide/components/intents-filters.html"> |
| 30 | Intents and Intent Filters |
| 31 | </a> |
| 32 | </li> |
| 33 | <li> |
| 34 | <a href="{@docRoot}design/patterns/notifications.html">Notifications</a> Design Guide |
| 35 | </li> |
| 36 | </ul> |
| 37 | |
| 38 | |
| 39 | </div> |
| 40 | </div> |
| 41 | |
| 42 | |
| 43 | |
| 44 | <p> |
| 45 | Notifications can include an animated progress indicator that shows users the status |
| 46 | of an ongoing operation. If you can estimate how long the operation takes and how much of it |
| 47 | is complete at any time, use the "determinate" form of the indicator |
| 48 | (a progress bar). If you can't estimate the length of the operation, use the |
| 49 | "indeterminate" form of the indicator (an activity indicator). |
| 50 | </p> |
| 51 | <p> |
| 52 | Progress indicators are displayed with the platform's implementation of the |
| 53 | {@link android.widget.ProgressBar} class. |
| 54 | </p> |
| 55 | <p> |
| 56 | To use a progress indicator, call |
| 57 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()}. The |
| 58 | determinate and indeterminate forms are described in the following sections. |
| 59 | </p> |
| 60 | <!-- ------------------------------------------------------------------------------------------ --> |
| 61 | <h2 id="FixedProgress">Display a Fixed-duration Progress Indicator</h2> |
| 62 | <p> |
| 63 | To display a determinate progress bar, add the bar to your notification by calling |
| 64 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress |
| 65 | setProgress(max, progress, false)} and then issue the notification. |
| 66 | The third argument is a boolean that indicates whether the |
| 67 | progress bar is indeterminate (<strong>true</strong>) or determinate (<strong>false</strong>). |
| 68 | As your operation proceeds, |
| 69 | increment <code>progress</code>, and update the notification. At the end of the operation, |
| 70 | <code>progress</code> should equal <code>max</code>. A common way to call |
| 71 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()} |
| 72 | is to set <code>max</code> to 100 and then increment <code>progress</code> as a |
| 73 | "percent complete" value for the operation. |
| 74 | </p> |
| 75 | <p> |
| 76 | You can either leave the progress bar showing when the operation is done, or remove it. In |
| 77 | either case, remember to update the notification text to show that the operation is complete. |
| 78 | To remove the progress bar, call |
| 79 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress |
| 80 | setProgress(0, 0, false)}. For example: |
| 81 | </p> |
| 82 | <pre> |
| 83 | ... |
| 84 | mNotifyManager = |
| 85 | (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 86 | mBuilder = new NotificationCompat.Builder(this); |
| 87 | mBuilder.setContentTitle("Picture Download") |
| 88 | .setContentText("Download in progress") |
| 89 | .setSmallIcon(R.drawable.ic_notification); |
| 90 | // Start a lengthy operation in a background thread |
| 91 | new Thread( |
| 92 | new Runnable() { |
| 93 | @Override |
| 94 | public void run() { |
| 95 | int incr; |
| 96 | // Do the "lengthy" operation 20 times |
| 97 | for (incr = 0; incr <= 100; incr+=5) { |
| 98 | // Sets the progress indicator to a max value, the |
| 99 | // current completion percentage, and "determinate" |
| 100 | // state |
| 101 | mBuilder.setProgress(100, incr, false); |
| 102 | // Displays the progress bar for the first time. |
| 103 | mNotifyManager.notify(0, mBuilder.build()); |
| 104 | // Sleeps the thread, simulating an operation |
| 105 | // that takes time |
| 106 | try { |
| 107 | // Sleep for 5 seconds |
| 108 | Thread.sleep(5*1000); |
| 109 | } catch (InterruptedException e) { |
| 110 | Log.d(TAG, "sleep failure"); |
| 111 | } |
| 112 | } |
| 113 | // When the loop is finished, updates the notification |
| 114 | mBuilder.setContentText("Download complete") |
| 115 | // Removes the progress bar |
| 116 | .setProgress(0,0,false); |
| 117 | mNotifyManager.notify(ID, mBuilder.build()); |
| 118 | } |
| 119 | } |
| 120 | // Starts the thread by calling the run() method in its Runnable |
| 121 | ).start(); |
| 122 | </pre> |
| 123 | <p> |
| 124 | The resulting notifications are shown in figure 1. On the left side is a snapshot of the |
| 125 | notification during the operation; on the right side is a snapshot of it after the operation |
| 126 | has finished. |
| 127 | </p> |
| 128 | <img |
| 129 | id="figure1" |
| 130 | src="{@docRoot}images/ui/notifications/progress_bar_summary.png" |
| 131 | height="84" |
| 132 | alt="" /> |
| 133 | <p class="img-caption"> |
| 134 | <strong>Figure 1.</strong> The progress bar during and after the operation.</p> |
| 135 | <!-- ------------------------------------------------------------------------------------------ --> |
| 136 | <h2 id="ActivityIndicator">Display a Continuing Activity Indicator</h2> |
| 137 | <p> |
| 138 | To display a continuing (indeterminate) activity indicator, add it to your notification with |
| 139 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress(0, 0, true)} |
| 140 | and issue the notification. The first two arguments are ignored, and the third argument |
| 141 | declares that the indicator is indeterminate. The result is an indicator |
| 142 | that has the same style as a progress bar, except that its animation is ongoing. |
| 143 | </p> |
| 144 | <p> |
| 145 | Issue the notification at the beginning of the operation. The animation will run until you |
| 146 | modify your notification. When the operation is done, call |
| 147 | {@link android.support.v4.app.NotificationCompat.Builder#setProgress |
| 148 | setProgress(0, 0, false)} and then update the notification to remove the activity indicator. |
| 149 | Always do this; otherwise, the animation will run even when the operation is complete. Also |
| 150 | remember to change the notification text to indicate that the operation is complete. |
| 151 | </p> |
| 152 | <p> |
| 153 | To see how continuing activity indicators work, refer to the preceding snippet. Locate the following lines: |
| 154 | </p> |
| 155 | <pre> |
| 156 | // Sets the progress indicator to a max value, the current completion |
| 157 | // percentage, and "determinate" state |
| 158 | mBuilder.setProgress(100, incr, false); |
| 159 | // Issues the notification |
| 160 | mNotifyManager.notify(0, mBuilder.build()); |
| 161 | </pre> |
| 162 | <p> |
| 163 | Replace the lines you've found with the following lines. Notice that the third parameter |
| 164 | in the {@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()} |
| 165 | call is set to {@code true} to indicate that the progress bar is |
| 166 | indeterminate: |
| 167 | </p> |
| 168 | <pre> |
| 169 | // Sets an activity indicator for an operation of indeterminate length |
| 170 | mBuilder.setProgress(0, 0, true); |
| 171 | // Issues the notification |
| 172 | mNotifyManager.notify(0, mBuilder.build()); |
| 173 | </pre> |
| 174 | <p> |
| 175 | The resulting indicator is shown in figure 2: |
| 176 | </p> |
| 177 | <img |
| 178 | id="figure2" |
| 179 | src="{@docRoot}images/ui/notifications/activity_indicator.png" |
| 180 | height="99" |
| 181 | alt="" /> |
| 182 | <p class="img-caption"><strong>Figure 2.</strong> An ongoing activity indicator.</p> |