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);//发送消息,下载完成,}}});}}}
原创粉丝点击