Notification使用详解之二:可更新进度的通知

上次和大家分享了关于Notification的基础应用,包括简单的通知和自定义视图的通知。今天和大家分享一下如何实现一个可更新进度的通知。

我们将会模拟一个下载任务,先启动一个线程负责模拟下载工作,在这个过程中更新进度信息,然后下载线程把最新的进度信息以消息的形式,发送到UI线程的消息队列中,最后UI线程负责根据最新的进度信息来更新进度通知的UI界面。

好,大概就是这个步骤。接下来我们根据具体的实例来演示一下这个过程。

我们新建一个notification项目,然后修改/res/layout/main.xml布局文件,代码如下:

[html] view plain copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Button
  6. android:id="@+id/download"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="download"
  10. android:onClick="download"/>
  11. <Button
  12. android:id="@+id/cancel"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="cancel"
  16. android:onClick="cancel"/>
  17. </LinearLayout>

注意,为了使示例中的Java代码看起来结构更加清晰,我们将按钮的点击事件都定义在布局文件中。

然后再来看一下MainActivity.java的代码:

[java] view plain copy

  1. package com.scott.notification;

  2. import android.app.Activity;

  3. import android.app.Notification;

  4. import android.app.NotificationManager;

  5. import android.app.PendingIntent;

  6. import android.content.Context;

  7. import android.content.Intent;

  8. import android.os.Bundle;

  9. import android.os.Handler;

  10. import android.os.Message;

  11. import android.view.View;

  12. import android.widget.RemoteViews;

  13. public class MainActivity extends Activity {

  14. private static final int NOTIFY_ID = 0;

  15. private boolean cancelled;

  16. private NotificationManager mNotificationManager;

  17. private Notification mNotification;

  18. private Context mContext = this;

  19. private Handler handler = new Handler() {

  20. public void handleMessage(android.os.Message msg) {

  21. switch (msg.what) {

  22. case 1:

  23. int rate = msg.arg1;

  24. if (rate < 100) {

  25. // 更新进度

  26. RemoteViews contentView = mNotification.contentView;

  27. contentView.setTextViewText(R.id.rate, rate + "%");

  28. contentView.setProgressBar(R.id.progress, 100, rate, false);

  29. } else {

  30. // 下载完毕后变换通知形式

  31. mNotification.flags = Notification.FLAG_AUTO_CANCEL;

  32. mNotification.contentView = null;

  33. Intent intent = new Intent(mContext, FileMgrActivity.class);

  34. PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

  35. mNotification.setLatestEventInfo(mContext, "下载完成", "文件已下载完毕", contentIntent);

  36. }

  37. // 最后别忘了通知一下,否则不会更新

  38. mNotificationManager.notify(NOTIFY_ID, mNotification);

  39. break;

  40. case 0:

  41. // 取消通知

  42. mNotificationManager.cancel(NOTIFY_ID);

  43. break;

  44. }

  45. };

  46. };

  47. @Override

  48. public void onCreate(Bundle savedInstanceState) {

  49. super.onCreate(savedInstanceState);

  50. setContentView(R.layout.main);

  51. }

  52. public void download(View view) {

  53. mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  54. int icon = R.drawable.down;

  55. CharSequence tickerText = "开始下载";

  56. long when = System.currentTimeMillis();

  57. mNotification = new Notification(icon, tickerText, when);

  58. // 放置在"正在运行"栏目中

  59. mNotification.flags = Notification.FLAG_ONGOING_EVENT;

  60. RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.download_notification_layout);

  61. contentView.setTextViewText(R.id.fileName, "AngryBird.apk");

  62. // 指定个性化视图

  63. mNotification.contentView = contentView;

  64. // intent为null,表示点击通知时不跳转

  65. PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, null, 0);

  66. // 指定内容意图

  67. mNotification.contentIntent = contentIntent;

  68. mNotificationManager.notify(NOTIFY_ID, mNotification);

  69. new Thread() {

  70. public void run() {

  71. startDownload();

  72. };

  73. }.start();

  74. }

  75. public void cancel(View view) {

  76. cancelled = true;

  77. }

  78. private void startDownload() {

  79. cancelled = false;

  80. int rate = 0;

  81. while (!cancelled && rate < 100) {

  82. try {

  83. // 模拟下载进度

  84. Thread.sleep(500);

  85. rate = rate + 5;

  86. } catch (InterruptedException e) {

  87. e.printStackTrace();

  88. }

  89. Message msg = handler.obtainMessage();

  90. msg.what = 1;

  91. msg.arg1 = rate;

  92. handler.sendMessage(msg);

  93. }

  94. if (cancelled) {

  95. Message msg = handler.obtainMessage();

  96. msg.what = 0;

  97. handler.sendMessage(msg);

  98. }

  99. }

  100. }

值 得注意的是,在控制下载线程时使用了cancelled这个boolean值标志变量,对于线程的控制更好一些,不建议使用 Thread.interrupt()去中断一个线程。另外,对于更新通知的UI界面时,要记住调用 NotificationManager.notify(int id, Notification notification)方法通知一下,否则即使设置了新值,也不会起作用的。

程序中用到的带进度的通知布局/res/layout/download_notification_layout.xml布局文件代码如下:

[html] view plain copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:padding="3dp">
  5. <ImageView
  6. android:id="@+id/imageView"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_margin="3dp"
  10. android:src="@drawable/down"/>
  11. <TextView
  12. android:id="@+id/fileName"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_toRightOf="@id/imageView"
  16. android:layout_alignBottom="@id/imageView"
  17. android:gravity="center_vertical"
  18. android:textColor="#000"/>
  19. <TextView
  20. android:id="@+id/rate"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/imageView"
  24. android:layout_alignRight="@id/imageView"
  25. android:gravity="center"
  26. android:text="0%"
  27. android:textColor="#000"/>
  28. <ProgressBar
  29. android:id="@+id/progress"
  30. style="?android:attr/progressBarStyleHorizontal"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_below="@id/fileName"
  34. android:layout_alignLeft="@id/fileName"
  35. android:max="100"
  36. android:progress="0"/>
  37. </RelativeLayout>

该通知的布局使用了相对布局,更加灵活易用,所以推荐大家多使用相对布局。

对于MainActivity.java中涉及到的FileMgrActivity,它是一个简单的界面,这里就不在介绍了,that's not the point。

最后我们跑一下程序,看看效果如何:

貌似还不错,好了,今天先到这里,下次再找机会分享。

评论

暂无评论