当前位置: 首页 > news >正文

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 }

   效果如图:

 

转载于:https://www.cnblogs.com/liutianyi/archive/2012/09/25/2701105.html

相关文章:

  • 【Android】关于手势识别(TouchEvent/Gesture)
  • 新的开始
  • 深入分析 iBATIS 框架之系统架构与映射原理
  • 创建.NET应用程序所经历的步骤
  • 【OpenCV学习】Laplace变换(视频边界检测)
  • smarty里面如何使用jquery
  • BZOJ 2099 [Usaco2010 Dec]Letter 恐吓信
  • IOSUITableView展开隐藏资源
  • Flex结合java实现一个登录功能
  • winform窗体去掉标题头部的两种方式
  • Mac OS X背后的故事(十一)Mac OS X文件系统的来龙去脉(上)
  • java中异步计算之Future
  • string.Format以及IFormattable,IFormatProvider,ICustomFormatter
  • System.InvalidOperationException 异常
  • hdu 3818模拟
  • Django 博客开发教程 16 - 统计文章阅读量
  • Docker下部署自己的LNMP工作环境
  • extjs4学习之配置
  • JavaScript 基本功--面试宝典
  • Java知识点总结(JavaIO-打印流)
  • linux安装openssl、swoole等扩展的具体步骤
  • PHP的类修饰符与访问修饰符
  • react 代码优化(一) ——事件处理
  • Selenium实战教程系列(二)---元素定位
  • SQLServer之创建数据库快照
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 给新手的新浪微博 SDK 集成教程【一】
  • 工作中总结前端开发流程--vue项目
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 盘点那些不知名却常用的 Git 操作
  • 前端学习笔记之观察者模式
  • 如何使用 JavaScript 解析 URL
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 思维导图—你不知道的JavaScript中卷
  • 再谈express与koa的对比
  • 在Mac OS X上安装 Ruby运行环境
  • 智能网联汽车信息安全
  • FaaS 的简单实践
  • postgresql行列转换函数
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • (2.2w字)前端单元测试之Jest详解篇
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (剑指Offer)面试题34:丑数
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (十三)Maven插件解析运行机制
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (转)http协议
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .net framework 4.0中如何 输出 form 的name属性。
  • .net 无限分类
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)