package com.bwie.asus.download.net; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.SystemClock; import android.util.Log; import java.io.File; import java.io.IOException; /** * Created by ASUS on 2017/11/12. */ public class DialogUtils { public static int MAX=0; public static int PROGRESS=-2; public static void ShowUpdateProgress(final Context context){ AlertDialog builder=new AlertDialog.Builder(context) .setTitle("版本有点低,请选择更新最新版本") .setPositiveButton("更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { new DownLoadThread("http://169.254.168.122:8080/qin/jinritoutiao_460.apk",context.getCacheDir()+"/jinritoutiao_460.apk",4).start(); showDownProgress(context,context.getCacheDir()+"/d.apk"); } }) .setNegativeButton("不用了",null) .show(); } private static void showDownProgress(final Context context, final String fileurl) { final ProgressDialog dialog=new ProgressDialog(context); dialog.setTitle("正在更新"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setMax(100); dialog.show(); new AsyncTask<String, Integer, String>() { @SuppressLint("LongLogTag") @Override protected String doInBackground(String... strings) { Log.i("==========DialogUtils========", "doInBackground:+Progress: "+PROGRESS+"Max:"+MAX); while (PROGRESS<MAX) { Log.i("==========DialogUtils========", "doInBackground:+Progress: "+PROGRESS+"Max:"+MAX); SystemClock.sleep(100); if (MAX > 0) { publishProgress((int) (PROGRESS * 100 / MAX)); } else { publishProgress(0); } } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); dialog.dismiss(); File file=new File(fileurl); //重新给下载的文件设置权限 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); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setDataAndType(Uri.parse("file://"+fileurl),"application/vnd.android.package-archive"); context.startActivity(intent); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); dialog.setProgress(values[0]); } }.execute(); }}
package com.bwie.asus.download.net; /** * Created by ASUS on 2017/11/12. */ public class DownLoadTask extends Thread { private int DownSize; private String DownUrl; private int startPosition; private String path; public DownLoadTask(int downSize, String downUrl, int startPosition, String path) { DownSize = downSize; DownUrl = downUrl; this.startPosition = startPosition; this.path = path; } @Override public void run() { super.run(); NetUtils.DownLoad(DownUrl,DownSize,path,startPosition); } }
package com.bwie.asus.download.net; import android.annotation.SuppressLint; import android.util.Log; /** * Created by ASUS on 2017/11/12. */ public class DownLoadThread extends Thread { private String downUrl=""; private int ThreadNum=5; private String path; public DownLoadThread(String downUrl,String path,int threadNum){ this.downUrl=downUrl; this.path=path; this.ThreadNum=threadNum; } @SuppressLint("LongLogTag") @Override public void run() { super.run(); //获取要下载文件的长度,即大小 int fileLength = NetUtils.getFileContentLength(downUrl); DialogUtils.MAX=fileLength; Log.i("=============DownLoadThread===============", "fileLength: "+fileLength); int DownSize=fileLength/ThreadNum; for (int i=0;i<ThreadNum;i++){ int startPosition=i*DownSize; if (i==ThreadNum-1) { DownSize=fileLength-DownSize*(ThreadNum-1); } new DownLoadTask(DownSize,downUrl,startPosition,path).start(); } } }package com.bwie.asus.download.net; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; /** * Created by ASUS on 2017/11/9. */ public class NetUtils { /** * 获取要下的文件的长度 * @param downloadUrl * @return */ public static int getFileContentLength(String downloadUrl){ int contentLength=0; try { URL url=new URL(downloadUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.setConnectTimeout(5000); //获取下载的长度 contentLength = con.getContentLength(); } catch (Exception e) { e.printStackTrace(); } return contentLength; } public static void DownLoad(String downUrl,int DownSize,String path,int startPosition){ BufferedInputStream buffer = null; RandomAccessFile raf = null; try { File file=new File(path); if (!file.exists()) { file.createNewFile(); } URL url=new URL(downUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.setReadTimeout(5000); if (DownSize>0) { int endPosition=DownSize+startPosition-1; con.setRequestProperty("Range","bytes="+startPosition+"-"+endPosition); } int responseCode = con.getResponseCode();//获取响应码 if (400>responseCode) { buffer = new BufferedInputStream(con.getInputStream()); raf = new RandomAccessFile(file, "rwd"); raf.seek(startPosition); int len=0; byte[] bytes=new byte[1024*8]; while ((len=buffer.read(bytes))!=-1){ synchronized (NetUtils.class){ raf.write(bytes,0,len); if (DialogUtils.PROGRESS<0) { DialogUtils.PROGRESS=0; } DialogUtils.PROGRESS+=len; } } } } catch (Exception e) { e.printStackTrace(); }finally { if (buffer!=null) { try { buffer.close(); } catch (IOException e) { e.printStackTrace(); } } if (raf!=null) { try { raf.close(); } catch (IOException e) { e.printStackTrace(); } } } } }package com.bwie.asus.download; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.bwie.asus.download.net.DialogUtils; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // DialogUtils.ShowUpdateProgress(this); } }
多线程下载
最新推荐文章于 2024-03-10 23:38:03 发布