android下载断点续传
来源:互联网 发布:tensorflow训练mnist 编辑:程序博客网 时间:2024/06/16 10:25
先贴图 开始下载
暂停下载
![这里写图片描述](http://img.blog.csdn.net/20160729163300555)
杀进程或关闭activity后,再次启动可正常下载,不会做动态图,这里就不贴图了
下载完成后,红框标记即为下载后文件
MainActivit里onCreate()里代码如下,主要做一些变量和UI的初始化工具:
public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_main); pbBar = (ProgressBar) findViewById(R.id.progressBar); tv = (TextView) findViewById(R.id.textView); bt_start= (Button) findViewById(R.id.button); File file = new File(filePath); dao = new DownInfoDao(this); DownInfo info = dao.query(path); Log.i("guoguo", info+"=info"); if (info == null) { isFinished=false; if (file.exists()) { file.delete(); Log.i("guoguo", "删除文件"); } } else { fileSize=info.getTotal(); downlen = info.getDownLen(); if (fileSize>0) { progress = (int)(((float)downlen /(float) fileSize)* 100) ; if (downlen==fileSize) { isFinished=true; } } if (!file.exists()) { dao.delete(path); } pbBar.setProgress(progress); tv.setText(progress + "%"); } Intent it = new Intent(this, DownloadService.class); bindService(it, conn, Context.BIND_AUTO_CREATE); startService(it); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
更新回调及handler:
private ICallbackResult callback = new ICallbackResult() { @Override public void OnBackResult(int total, int downlen) { Log.i("guoguo", "downlen="+downlen+",total="+total); int pro = (int)(((float)downlen /(float) total)* 100) ; Message msg = new Message(); msg.what = 1; msg.obj = pro; handler.sendMessage(msg); } }; Handler handler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: int progress=(Integer) msg.obj; pbBar.setProgress(progress); if (progress==100) { isFinished=true; } tv.setText(progress+"%"); break; default: break; } }; };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
核心代码来了,主要工作在service里完成,以下是文件的下载部分,:
try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); DownInfo info = dao.query(path); if (info==null) { info=new DownInfo(path, 0, fileSize); dao.insert(info); }else { downlen=info.getDownLen(); fileSize=info.getTotal(); conn.setRequestProperty("Range", "bytes=" + downlen + "-" + fileSize); } conn.connect(); int length = conn.getContentLength(); if (fileSize==0) { fileSize=length; } InputStream is = conn.getInputStream(); File file = new File(savePath); if (!file.exists()) { file.mkdirs(); } RandomAccessFile apkFile = new RandomAccessFile(saveFileName,"rwd"); apkFile.seek(downlen); byte buf[] = new byte[1024*1024]; do { while (isPause) { synchronized (dao) { try { dao.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } int numread = is.read(buf); if (numread > 0) { downlen += numread; apkFile.write(buf, 0, numread); }else { Message msg = mHandler.obtainMessage(); msg.what = 1; mHandler.sendEmptyMessage(0); canceled = true; break; } } while (!canceled); apkFile.close(); is.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
现在为文件的更新部分
url = new URL(path); URLConnection conn = url.openConnection(); fileSize = conn.getContentLength(); if (downLoadThread == null || !downLoadThread.isAlive()) { downLoadThread = new Thread(mdownApkRunnable); downLoadThread.start(); } DownInfo info = dao.query(path); if (info != null) { downlen = info.getDownLen(); fileSize=info.getTotal(); } while (!flag) { while (isPause) { synchronized (dao) { try { dao.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } if (downlen==fileSize||isPause) { flag=true; } dao.update(new DownInfo(path, downlen,fileSize)); callback.OnBackResult(fileSize,downlen); Thread.sleep(200); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
下载和更新工作如果在同一个线程里处理里下载速度会非常慢,所以我把两个工作分开来处理,这样能保证下载速度 。
0 0