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

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

        先写自定义布局bestinfo_download_notifi.xml:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

    <ImageView
            android:id="@+id/tv_download_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/filetype_ppt" />

    <LinearLayout
            android:layout_marginLeft="5dp"
            android:orientation="vertical"
            android:layout_alignBottom="@+id/tv_download_icon"
            android:layout_alignTop="@+id/tv_download_icon"
            android:layout_toRightOf="@+id/tv_download_icon"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TextView
                android:layout_marginTop="5dp"
                android:singleLine="true"
                android:id="@+id/tv_download_title"
                android:text="你想要的资料"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:id="@+id/tv_download_progressBar"
                android:layout_marginTop="3dp"
                android:layout_marginRight="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        <TextView
                android:id="@+id/tv_download_progress"
                android:text="10%"
                android:layout_marginTop="3dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>

</RelativeLayout></span>
        效果:



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

<span style="font-size:14px;">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<File>() {
                            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<File> 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
                            }
                        });
            }
        });</span>

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






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值