Notification案例- 多个任务单线程下载共用一个notification显示进度

在项目中有这样一个需求,如图总共有五个下载任务,要在一个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(){

		@Override
		public 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);//发送消息,下载完成,
					}
				}
			
			});
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值