多线程下载视频
来源:互联网 发布:windows平板电脑 编辑:程序博客网 时间:2024/06/05 14:25
//加网络权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>
DialogUtilspublic static long MAX_SIZE = 0; public static long PROGRESS = -2; public static void showUpdataDialog(final Context context){ AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("是否更新"); builder.setMessage("太旧了,更新吧"); builder.setNegativeButton("就不", null); builder.setPositiveButton("可以", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { new DownLoadThread("http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4", context.getCacheDir() + "911Mothers_2010W-480p.mp4").start(); showProgress(context); } }); builder.show(); } public static void showProgress(final Context context){ final ProgressDialog pd = new ProgressDialog(context); pd.setTitle("正在更新"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(100); pd.show(); new AsyncTask<String, Integer, String>(){ @Override protected String doInBackground(String... strings) { while (PROGRESS+1 < MAX_SIZE){ SystemClock.sleep(100); if(MAX_SIZE>0) { publishProgress((int)(PROGRESS * 100 / MAX_SIZE)); } } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); pd.dismiss();// File file = new File(context.getCacheDir() + "/taobao_161.apk");// String[] command = {"chmod", "777", file.getPath() };// ProcessBuilder builder = new ProcessBuilder(command);// try {// builder.start();// } catch (IOException e) {// e.printStackTrace();// }////// Intent intent = new Intent(Intent.ACTION_VIEW);//// 由于没有在Activity环境下启动Activity,设置下面的标签// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// intent.setDataAndType(Uri.fromFile(file)),// "application/vnd.android.package-archive"),// context.startActivity(intent); File file = new File(context.getCacheDir() + "/app-release.apk"); String command = "chmod " + "777" + " " + file.getPath(); Runtime runtime = Runtime.getRuntime(); try { runtime.exec(command); } catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent(Intent.ACTION_VIEW);// 由于没有在Activity环境下启动Activity,设置下面的标签 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");context.startActivity(intent); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); pd.setProgress(values[0]); } }.execute(); }DownLoadTask
String downLoadUrl;String path;int blockSize;int startPosition;public DownLoadTask(String downLoadUrl, String path, int blockSize, int startPosition) { this.downLoadUrl = downLoadUrl; this.path = path; this.blockSize = blockSize; this.startPosition = startPosition;}@Overridepublic void run() { super.run(); NetUtils.downloadFile(downLoadUrl,path,blockSize,startPosition);}DownLoadThreadprivate String downLoadUrl = "";private String path;private int threadNum = 5;public DownLoadThread(String downLoadUrl, String path) { this.downLoadUrl = downLoadUrl; this.path = path; this.threadNum = threadNum;}@Overridepublic void run() { int len = NetUtils.getFileLength(downLoadUrl); DialogUtils.MAX_SIZE = len; int blockSize = len / threadNum; for (int i = 0; i < threadNum; i++) { int startPosition = i * blockSize; if (i == threadNum - 1) { blockSize = len - blockSize * (threadNum - 1); } new DownLoadTask(downLoadUrl, path, blockSize, startPosition).start(); }}NetUtilsMain 类public static void downloadFile(String downloadUrl, String path, int blockSize, int startPosition){ BufferedInputStream bis = null; RandomAccessFile raf = null; try { File f = new File(path); if(!f.exists()){ f.createNewFile(); } URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(8000); conn.setConnectTimeout(8000);// long start = 0; if(blockSize > 0){// //使用线程id来计算 当前线程的开始位置和结束位置// start = blockSize * threadId; long end = blockSize + startPosition - 1; //多线程下载 需要告诉服务器我要请求的是哪部分内容 需要写在请求头里面 conn.setRequestProperty("Range", "bytes=" + startPosition + "-" + end); Log.i(Thread.currentThread() + "======", "bytes=" + startPosition + "-" + end); } int code = conn.getResponseCode(); if(code < 400){ bis = new BufferedInputStream(conn.getInputStream()); raf = new RandomAccessFile(f, "rwd"); // raf.seek(startPosition); // int len = 0; byte[] buff = new byte[1024 * 8]; while((len = bis.read(buff)) != -1){ synchronized (NetUtils.class){ raf.write(buff, 0, len); if(DialogUtils.PROGRESS<0) { DialogUtils.PROGRESS=0; } DialogUtils.PROGRESS += len; } } } } catch (Exception e) { e.printStackTrace(); }finally { if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(raf != null){ try { raf.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static int getFileLength(String downloadUrl){ int len = 0; try { URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(8000); conn.setConnectTimeout(8000); len = conn.getContentLength(); } catch (Exception e) { e.printStackTrace(); } return len; }public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DialogUtils.showUpdataDialog(this); }}
阅读全文
0 0
- 多线程下载加载视频
- 多线程下载视频
- 多线程下载+视频播放
- 多线程下载视频
- 用多线程下载视频(Java)
- 多线程下载+断点续传+播放视频
- Retrofit+RxJava+Mvp实现多线程视频下载
- Retrofit+RXJava_多线程下载视频列表
- 最新GreenDao多线程下载,断点续传,播放视频
- Retrofit请求数据 多线程下载 Ijkplayer视频播放
- 多线程下载播放视频,Retorfit+RXjava+Banner点击条目播放视频
- 视频下载
- 视频下载
- 下载视频
- 视频下载
- 多线程下载
- 多线程下载
- 多线程下载
- 优秀程序员的炼成记!
- swagger前后端使用说明
- Hibernate常用的接口和类Session&Transaction(四)
- 谷歌、微软、OpenAI 等巨头的机器学习开源项目
- (原创)Android6.0亮屏流程分析
- 多线程下载视频
- numpy.core.multiarray failed to import
- Java线程状态转换
- 朴素贝叶斯(Naive Bayes),“Naive”在何处?
- 传入当前的经纬度和目标经纬度,计算两点间距离
- Gluttony(codeforce-891B)
- Apache HttpComponents
- springMvc+Mybatis整合
- sublime使用技巧