显示Notification进度

编写:fastcome1985 - 原文:http://developer.android.com/training/notify-user/display-progress.html

Notifications可以包含一个展示用户正在进行的操作状态的动画进度指示器。如果你可以在任何时候估算这个操作得花多少时间以及当前已经完成多少,你可以用“determinate(确定的,下同)”形式的指示器(一个进度条)。如果你不能估算这个操作的长度,使用“indeterminate(不确定,下同)”形式的指示器(一个活动的指示器)。

进度指示器用ProgressBar平台实现类来显示。

使用进度指示器,可以调用 setProgress()方法。determinate 与 indeterminate形式将在下面的章节中介绍。

展示固定长度的进度指示器

为了展示一个确定长度的进度条,调用 setProgress(max, progress, false))方法将进度条添加进notification,然后发布这个notification,第三个参数是个boolean类型,决定进度条是 indeterminate (true) 还是 determinate (false)。在你操作进行时,增加progress,更新notification。在操作结束时,progress应该等于max。一个常用的调用 setProgress())的方法是设置max为100,然后增加progress就像操作的“完成百分比”。

当操作完成的时候,你可以选择或者让进度条继续展示,或者移除它。无论哪种情况下,记得更新notification的文字来显示操作完成。移除进度条,调用setProgress(0, 0, false))方法.比如:

  1. int id = 1;
  2. ...
  3. mNotifyManager =
  4. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  5. mBuilder = new NotificationCompat.Builder(this);
  6. mBuilder.setContentTitle("Picture Download")
  7. .setContentText("Download in progress")
  8. .setSmallIcon(R.drawable.ic_notification);
  9. // Start a lengthy operation in a background thread
  10. new Thread(
  11. new Runnable() {
  12. @Override
  13. public void run() {
  14. int incr;
  15. // Do the "lengthy" operation 20 times
  16. for (incr = 0; incr <= 100; incr+=5) {
  17. // Sets the progress indicator to a max value, the
  18. // current completion percentage, and "determinate"
  19. // state
  20. mBuilder.setProgress(100, incr, false);
  21. // Displays the progress bar for the first time.
  22. mNotifyManager.notify(id, mBuilder.build());
  23. // Sleeps the thread, simulating an operation
  24. // that takes time
  25. try {
  26. // Sleep for 5 seconds
  27. Thread.sleep(5*1000);
  28. } catch (InterruptedException e) {
  29. Log.d(TAG, "sleep failure");
  30. }
  31. }
  32. // When the loop is finished, updates the notification
  33. mBuilder.setContentText("Download complete")
  34. // Removes the progress bar
  35. .setProgress(0,0,false);
  36. mNotifyManager.notify(id, mBuilder.build());
  37. }
  38. }
  39. // Starts the thread by calling the run() method in its Runnable
  40. ).start();

结果notifications显示在图1中,左边是操作正在进行中的notification的快照,右边是操作已经完成的notification的快照。

fragments-screen-mock 图1 操作正在进行中与完成时的进度条

展示持续的活动的指示器

为了展示一个持续的(indeterminate)活动的指示器,用setProgress(0, 0, true))方法把指示器添加进notification,然后发布这个notification 。前两个参数忽略,第三个参数决定indicator 还是 indeterminate。结果是指示器与进度条有同样的样式,除了它的动画正在进行。

在操作开始的时候发布notification,动画将会一直进行直到你更新notification。当操作完成时,调用 setProgress(0, 0, false)) 方法,然后更新notification来移除这个动画指示器。一定要这么做,否责即使你操作完成了,动画还是会在那运行。同时也要记得更新notification的文字来显示操作完成。

为了观察持续的活动的指示器是如何工作的,看前面的代码。定位到下面的几行:

  1. // Sets the progress indicator to a max value, the current completion
  2. // percentage, and "determinate" state
  3. mBuilder.setProgress(100, incr, false);
  4. // Issues the notification
  5. mNotifyManager.notify(id, mBuilder.build());

将你找到的代码用下面的几行代码代替,注意 setProgress()方法的第三个参数设置成了true,表示进度条是 indeterminate类型的。

  1. // Sets an activity indicator for an operation of indeterminate length
  2. mBuilder.setProgress(0, 0, true);
  3. // Issues the notification
  4. mNotifyManager.notify(id, mBuilder.build());

结果显示在图2中:

fragments-screen-mock

图2 正在进行的活动的指示器