Notification中显示进度条

      程序功能:在Notification中显示进度条,并随着任务完成进度更改进度条的进度,下面详细说明:

      我们要在Notification中显示进度条,就要修改我其中的布局,首先准备一个布局文件,如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/noti_tv"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="15#" />
12 
13     <ProgressBar
14         android:id="@+id/noti_pd"
15         style="?android:attr/progressBarStyleHorizontal"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:layout_alignLeft="@id/noti_tv"
19         android:layout_below="@id/noti_tv" />
20 
21 </RelativeLayout>

        布局很简单,一个TextView(用来显示进度百分比的文字描述)和一个ProgressBar(用来显示进度条),如果想把这个布局文件和Notification相关联,我们换需要用到RemoteViews这个类,通过RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti)就可以把noti.xml布局文件转化为程序中的View,然后notification.contentView = view 就可以改变Notification的布局了。代码如下:

 1     private void init() {
 2         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 3         notification = new Notification(R.drawable.ic_launcher, "下载", System
 4                 .currentTimeMillis());
 5 
 6         RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti);
 7         notification.contentView = view;
 8 
 9         PendingIntent contentIntent = PendingIntent.getActivity(this,
10                 R.string.app_name, new Intent(),
11                 PendingIntent.FLAG_UPDATE_CURRENT);
12 
13         notification.contentIntent = contentIntent;
14     }

       这样我们就成功的修改了Notification的布局,我们只要给变其中TextView和ProgressBar的内容就可以实现我们的要求,要改变新布局中控件的内容,要改变TextView的内容我们需要用到notification.contentView.setTextViewText(R.id.noti_tv,"");此方法需要用到两恶参数,其中noti_tv是我们布局文件中的TextView的Id,第二个参数是要显示的内容;要改变ProgressBar的内容我们用到notification.contentView.setProgressBar(R.id.noti_pd, 100,int, false);此方法需要四个参数第,noti_pd为ProgressBar在布局文件中Id,第二个参数设置进度条的最大值,第三个参数是当前进度条要显示的值,第四个参数为设置进度条是模糊的还是精准的,false是精准,true是模糊,我们应用中大部分都是精准显示,用户一目了然,如果设置成模糊的话,我们则看不到进度条的变化。

 1     class downLoadTask extends AsyncTask<Void, Void, Void> {
 2 
 3         @Override
 4         protected Void doInBackground(Void... params) {
 5 
 6             for (int i = 0; i <= 100; i++) {
 7 
 8                 // 为了避免频繁发送消息所以每次增长10
 9                 if (i % 10 == 0) {
10                     try {
11                         // 模拟耗时操作
12                         Thread.sleep(1000);
13                     } catch (InterruptedException e) {
14                         e.printStackTrace();
15                     }
16                     // 更改文字
17                     notification.contentView.setTextViewText(R.id.noti_tv, i
18                             + "%");
19                     // 更改进度条
20                     notification.contentView.setProgressBar(R.id.noti_pd, 100,
21                             i, false);
22                     // 发送消息
23                     notificationManager.notify(0, notification);
24                 }
25             }
26 
27             return null;
28         }

    下面,只要我们在OnCreate方法中启动downLoadTask (我们也可以放到Service去做后台耗时操作)便可以看到我们想要的结果了,详细代码如下:

完整代码
 1 public class NotiDemo extends Activity {
 2 
 3     private NotificationManager notificationManager;
 4     private Notification notification;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8 
 9         super.onCreate(savedInstanceState);
10 
11         init();
12         // 开启子线程
13         new downLoadTask().execute();
14 
15     }
16 
17     /**
18      * 初始化
19      */
20     private void init() {
21         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
22         notification = new Notification(R.drawable.ic_launcher, "下载", System
23                 .currentTimeMillis());
24 
25         RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti);
26         notification.contentView = view;
27 
28         PendingIntent contentIntent = PendingIntent.getActivity(this,
29                 R.string.app_name, new Intent(),
30                 PendingIntent.FLAG_UPDATE_CURRENT);
31 
32         notification.contentIntent = contentIntent;
33     }
34 
35     /**
36      *子线程,用来更新Notification
37      */
38     class downLoadTask extends AsyncTask<Void, Void, Void> {
39 
40         @Override
41         protected Void doInBackground(Void... params) {
42 
43             for (int i = 0; i <= 100; i++) {
44 
45                 // 为了避免频繁发送消息所以每次增长10
46                 if (i % 10 == 0) {
47                     try {
48                         // 模拟耗时操作
49                         Thread.sleep(1000);
50                     } catch (InterruptedException e) {
51                         e.printStackTrace();
52                     }
53                     // 更改文字
54                     notification.contentView.setTextViewText(R.id.noti_tv, i
55                             + "%");
56                     // 更改进度条
57                     notification.contentView.setProgressBar(R.id.noti_pd, 100,
58                             i, false);
59                     // 发送消息
60                     notificationManager.notify(0, notification);
61                 }
62             }
63 
64             return null;
65         }
66     }
67 
68 }

   效果如图:

 

posted on 2012-09-25 10:02  liutianyi  阅读(4925)  评论(0编辑  收藏  举报