Notification中显示进度条

来源:互联网 发布:智能大厦网络设计方案 编辑:程序博客网 时间:2024/06/05 08:27

http://www.cnblogs.com/liutianyi/archive/2012/09/25/2701105.html

程序功能:在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     <ProgressBar14         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, i18                             + "%");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去做后台耗时操作)便可以看到我们想要的结果了,详细代码如下:
完整代码public class NotiDemo extends Activity {    private NotificationManager notificationManager;    private Notification notification;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        init();        // 开启子线程        new downLoadTask().execute();    }    /**     * 初始化     */    private void init() {        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        notification = new Notification(R.drawable.ic_launcher, "下载", System                .currentTimeMillis());        RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti);        notification.contentView = view;        PendingIntent contentIntent = PendingIntent.getActivity(this,                R.string.app_name, new Intent(),                PendingIntent.FLAG_UPDATE_CURRENT);        notification.contentIntent = contentIntent;    }    /**     *子线程,用来更新Notification     */    class downLoadTask extends AsyncTask<Void, Void, Void> {        @Override        protected Void doInBackground(Void... params) {            for (int i = 0; i <= 100; i++) {                // 为了避免频繁发送消息所以每次增长10                if (i % 10 == 0) {                    try {                        // 模拟耗时操作                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    // 更改文字                    notification.contentView.setTextViewText(R.id.noti_tv, i                            + "%");                    // 更改进度条                    notification.contentView.setProgressBar(R.id.noti_pd, 100,                            i, false);                    // 发送消息                    notificationManager.notify(0, notification);                }            }            return null;        }    }}
0 0
原创粉丝点击