HttpURLConnection实现文件下载,Service加Notification实现后台下载

一切从简

两个结合来讲解,使用服务和通知进行后台下载


一个页面,用于启动服务,上面只有一个Button,不列出布局文件了。


启动service


Intent integer=new Intent(Service_Main.this,Servier__.class);
startService(integer);
 
 
Servier__类中:
全局变量
int Add_Lenth;
下载进度(字节)
int Sudu;
下载速度(字节)
int Lenth;
文件大小(字节)
int Index;
下载的时间
文件的下载		           下载地址     文件名             本机保存的位置
public void Get_HttpUrl_File(String urlStr, String fileName, String savePath) throws IOException {
    HttpURLConnection conn=(HttpURLConnection) new URL(urlStr).openConnection();
//打开连接
    conn.setRequestMethod("GET");
//设置请求类型
    conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//设置请求头属性
    InputStream inputStream = conn.getInputStream();
//正式进行请求

    byte[] bytes=new byte[1024];
//一个1024的数组,用于保存下载的文件流
    int hasRea=0;
//用于临时保存文件流
    Timer timer=new Timer();
    timer.schedule(task,0,1000);
//开始下载便进行通知,1秒更新一次
    Lenth =conn.getContentLength();
//最大字节数
    RandomAccessFile currFile=new RandomAccessFile(savePath+fileName,"rw");
//文件保存区(非内存)直接对文件进行保存
while((hasRea=inputStream.read(bytes))>0){
//读取下载的文件流,
        currFile.write(bytes,0,hasRea);
//进行读写
        Add_Lenth+=hasRea;
//下载进度(字节)
        Sudu+=hasRea;
//下载速度(字节),之所以有两个是因为速度回被清空
    }

    currFile.close();
//关闭
    inputStream.close();
//关闭
}

//计时器

  TimerTask task=new TimerTask() {
        @Override
        public void run() {//子线程更新
            Index+=1;
//每秒加一
            NotificationManager manger= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通知控制器

//编写通知
Notification noti = new Notification.Builder(getApplicationContext())
                    .setAutoCancel(true)//单击消失
                    .setProgress(Lenth,Add_Lenth,false)//通知的进度条
                    .setContentTitle("下载中" )//标题
                    .setContentText(String.valueOf(Sudu/1024)+"K"+"----"+Index+"秒----------"+Add_Lenth+"/"+Lenth)
                    .setSmallIcon(R.mipmap.ic_launcher)//图片
                    .build();
            manger.notify(999, noti);//启用
            Sudu=0;//重置时间
        }
    };

onCreate函数
 @Override
    public void onCreate() {//启动服务进行的函数
        super.onCreate();
        Index=0;
//初始化时间
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String path = Environment.getExternalStorageDirectory().getPath() + "/AAAAImg/";
//获取目录
                try {
                    Get_HttpUrl_File("http://sqdownb.onlinedown.net/down/TaoBao_630.apk","baidu.apk",path);
//调用网络函数(下载地址,下载后的保存名字,目录)
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
//启动
    }

!!记得开网络权限!!
这里只做最初级的显示效果







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值