Android使用ProgressBar、自定义Notification显示文件下载进度

        好久没写安卓了,今天忙着做这个效果出来,本来打算去网上copy一段赶工,结果发现翻来覆去就那几篇文章,还是自己写一篇吧。

        先写自定义布局bestinfo_download_notifi.xml:




    

    
        
        
        
    

        效果:

Android使用ProgressBar、自定义Notification显示文件下载进度_第1张图片

        然后直接上主要的代码吧,这里用的是XUtils下载文件,主要就是几个回调函数,细节在注释中说明

iv_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isDownloading){//定义在Activity中的变量,标识是否正在下载
                    T.showShort(MainActivity.this,"文件正在下载");
                    return;
                }
                HttpUtils http = new HttpUtils();
                http.download(URL,
                        Environment.getExternalStorageDirectory()+"视频.mp4", true, false,
                        new RequestCallBack() {
                            private Notification notification;
                            private NotificationCompat.Builder mBuilder;
                            private RemoteViews view;//控制Notification自定义布局的对象
                            private NotificationManager notificationManager;

                            @Override
                            public void onStart() {//开始下载回调函数
                                isDownloading = true;
                                
                                notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                mBuilder = new NotificationCompat.Builder(Main.this)
                                       .setSmallIcon(getDrawableId())
                                        .setLargeIcon(getBitMap());//注意如果不设置icon,Notification将不会显示,其他项都在RemoteViews中设置。

                                view = new RemoteViews(getPackageName(), R.layout.bestinfo_download_notifi);//初始化RemoteViews对象,装载布局
                                mBuilder.setContent(view);//设置Notification的布局
                                notification = mBuilder.build();
                                notification.contentView.setImageViewBitmap(R.id.tv_download_icon,bitmap);//通过RemoteViews来设置Notification中的图片
                                notification.contentView.setTextViewText(R.id.tv_download_title,"this is title");//通过RemoteViews来设置Notification中的TextView
                                notificationManager.notify(R.string.app_name, notification);//完成Notification相关对象的构造,发起第一次通知

                            }

                            @Override
                            public void onLoading(long total, long current,
                                                  boolean isUploading) {//下载中的回调函数
                                super.onLoading(total, current, isUploading);
                                view.setTextViewText(R.id.tv_download_progress,(int) (current * 100 / total) + "%");//设置下载进度,这里注意,服务器端代码需要设置ContentLength,否则total为-1
                                view.setProgressBar(R.id.tv_download_progressBar,
                                        Integer.valueOf(total+""),Integer.valueOf(current+""),false);//通过RemoteViews来设置ProgressBar进度
				//通知
                                notification.contentView = view;
                                notificationManager.notify(R.string.app_name,notification);
                            }

                            @Override
                            public void onSuccess(ResponseInfo responseInfo) {//下载完成的回调函数

				//下载完成,设置Notification点击的PendingIntent
                                view.setTextViewText(R.id.tv_download_progress,"下载完成,点击打开文件");
                                Uri uri = Uri.fromFile(responseInfo.result);
                                Intent intent = new Intent("android.intent.action.VIEW");
                                intent.setDataAndType(uri, "vedio/*");
                                PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                                notification.contentIntent = resultPendingIntent;
                                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                notification.contentView = view;
                                notificationManager.notify(R.string.app_name,notification);
                                isDownloading = false;//重置下载flag
                            }

                            @Override
                            public void onFailure(HttpException error, String msg) {//下载失败的回调函数
                                T.showShort(MainActivity.this,"下载失败");
                                notification.contentView = view;
                                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                                notificationManager.notify(R.string.app_name,notification);
                                isDownloading = false;//重置下载flag
                            }
                        });
            }
        });

        我是真机调试,就不上效果图了,其他就不多说了,如有意见,多多指教。






你可能感兴趣的:(Android使用ProgressBar、自定义Notification显示文件下载进度)