Notification案例- 多个任务单线程下载共用一个notification显示进度
来源:互联网 发布:教育软件排名 编辑:程序博客网 时间:2024/05/16 06:22
在项目中有这样一个需求,如图总共有五个下载任务,要在一个notification上显示下载进度。。。。
实现思路:5个任务,5Runable 对象,添加到一个taskQueue然后逐个的创建线程,完成任务,在run方法里面不断发送消息,更新界面的数据。。。
在更新数据的时候记得重新设置notification.contentView=views; mNotificationManager.notify(R.id.progressbar, notification);(本人在这里花了点时间)
代码还有很有几个待优化的地方:1.runProgress方法里面避免多次创建线程,可以使用线程池
2.整体的代码进一步的封装
下面是主要实现代码;
public class JpushDemoActivity extends Activity { protected static final String TAG = "JpushDemoActivity";/** Called when the activity is first created. */Queue<Runnable> taskQueue=new LinkedList<Runnable>();Notification notification;NotificationManager mNotificationManager;RemoteViews views;int max=0;PendingIntent pendingIntent;Handler mHandler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case 1:max=(Integer) msg.obj;views.setProgressBar(R.id.progressbar, 100, max, false);views.setTextViewText(R.id.percent, max+"%");views.setTextViewText(R.id.number, msg.arg1+"/"+5);notification.contentIntent=pendingIntent;notification.contentView=views;mNotificationManager.notify(R.id.progressbar, notification);break;case 2://全部任务下载完成mNotificationManager.cancelAll(); break;case 3://运行新的任务runProgress();break;default:break;}}}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initTask(); initNotification(); runProgress(); }private void runProgress() { if(!taskQueue.isEmpty()){Runnable poll = taskQueue.poll();new Thread(poll).start();}}private void initNotification() { notification=new Notification(R.drawable.ic_launcher,//图标 "notification notification ",//信息 System.currentTimeMillis());//什么时候显示//notification.flags |= Notification.FLAG_NO_CLEAR;// 点击清除通知时候是否被清除notification.flags |= Notification.FLAG_ONGOING_EVENT; //允许处理点击事件 会显示在“正在进行中”notification.flags |= Notification.FLAG_AUTO_CANCEL; //点击后自动取消 views=new RemoteViews(this.getPackageName(), R.layout.notification_layout);views.setProgressBar(R.id.progressbar, 100, 0, false);notification.contentView=views; pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,//启动activity的 intent 注意是否需要 Intent.FLAG_ACTIVITY_NEW_TASK JpushDemoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);notification.contentIntent=pendingIntent;mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(R.id.progressbar, notification);//R.id.progressbar 设置唯一标识一个notification,就可以引用R.java文件中的常量}private void initTask() {for(int i=1;i<=5;i++){final int req=i;taskQueue.add(new Runnable() {int max=0;public void run() {while(max<100){try {max+=1;Message message = new Message();message.what=1;message.obj=max;message.arg1=req;mHandler.sendMessage(message);Thread.sleep(700);} catch (InterruptedException e) {e.printStackTrace();}}//mHandler.sendEmptyMessage(3);//一个任务完成,if(req==5){//这个5依据任务队列长度mHandler.sendEmptyMessage(2);//发送消息,下载完成,}}});}}}
- Notification案例- 多个任务单线程下载共用一个notification显示进度
- 多个Notification显示进度
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Android多任务下载,使用Notification显示、更新进度条
- Android实现Service下载文件,Notification显示下载进度
- 同时显示多个 Notification
- 【安卓笔记】在Notification中显示下载进度
- Android提示版本更新+notification显示下载进度
- Android中使用Notification实现应用更新显示下载进度
- 日常开发——Service, Notification显示下载进度
- Android使用ProgressBar、自定义Notification显示文件下载进度
- Service下载apk通过notification显示下载进度,下载完成自动安装
- 利用service下载图片&发送进度notification
- Notification案例
- android之apk自动更新采用Notification通知提示并显示下载进度
- android之apk自动更新采用Notification通知提示并显示下载进度
- javascript 函数,BOM
- 补银行问题
- android学习日记——hello word
- 当前多数杀软主防存在的缺陷
- MFC中编辑框的显示与隐藏
- Notification案例- 多个任务单线程下载共用一个notification显示进度
- mysql数据库中的索引有那些、有什么用?
- LSI设计(1)一般介绍(待上载)
- ASP.NET 动态创建TextBox 以及取值问题
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- windows下如何安装java6.0
- 游戏引擎列表
- assert()函数用法总结
- Android中Service与Thread的区别