我这里是 更新用的的,不说多的 上源码:
public class DownloadUtil { private final String TAG="DownloadUtil"; private static DownloadUtil downloadUtil; private final OkHttpClient okHttpClient; private DownloadUtil(){ okHttpClient=new OkHttpClient(); } public static DownloadUtil get() { if(downloadUtil==null) downloadUtil=new DownloadUtil(); return downloadUtil; } public void downLoad(final String url,final Context context,final OnDownloadListener listener){ Request request=new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { LogUtil.i(TAG,"onFailure"); listener.onDownloadFailed(); } @Override public void onResponse(Call call, Response response) throws IOException { LogUtil.i(TAG,"onResponse 1"); int lenght=0; byte[] buf=new byte[2048]; InputStream inputStream=null; FileOutputStream fileOutputStream=null; try { //存储下载文件目录 String savepath = context.getFilesDir().getAbsolutePath(); inputStream = response.body().byteStream(); long total = response.body().contentLength(); File file = new File(savepath, getNameFromUrl(url)); if(file.exists()) { file.delete(); } file.getParentFile().mkdirs(); getRoot("chmod -R 777 "+file.getAbsolutePath()); fileOutputStream = new FileOutputStream(file); long sum = 0; while ((lenght = inputStream.read(buf)) != -1) { fileOutputStream.write(buf,0,lenght); sum+=lenght; int progress=(int)(sum*1.0f/total*100); listener.onDownloading(progress); } fileOutputStream.flush(); listener.onDownloadSuccess(); LogUtil.i(TAG,"onResponse over"); }catch (Exception e){ listener.onDownloadFailed(); LogUtil.i(TAG,"onResponse Exception"+e.getMessage().toString()); }finally { try { if (inputStream != null) inputStream.close(); if (fileOutputStream != null) fileOutputStream.close(); }catch (Exception e){ } } LogUtil.i(TAG,"onResponse 2"); } }); } /** * 设置权限 * @param paramString * @return */ public synchronized boolean getRoot(String paramString) { Process process = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(paramString + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception localException) { System.out.println("@@@@root cmd error:"+localException); return false; }finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { System.out.println("###root cmd error:"+e); } } return true; } /** * 从下载链接中解析出文件名 * @param url * @return */ private String getNameFromUrl(String url){ return url.substring(url.lastIndexOf("/")+1); } public interface OnDownloadListener{ /** * 下载成功 */ void onDownloadSuccess(); /** * 下载进度 * @param progess */ void onDownloading(int progess); /* *下载失败 */ void onDownloadFailed(); } }
//网络 <uses-permission android:name="android.permission.INTERNET"/>
//Okhttp
compile 'com.squareup.okhttp3:okhttp:3.6.0'//调用
DownloadUtil.get().downLoad("你自己的下载地址", context, new DownloadUtil.OnDownloadListener() { @Override public void onDownloadSuccess(final File file) { ((Activity)context).runOnUiThread(new Runnable() { @Override public void run() { ToastUtil.getLongToast(context,R.string.update_success); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://"+file.getAbsolutePath()), "application/vnd.android.package-archive");//写的时候没有权限 所以加上权限 String[] command = {"chmod", "777", file.getAbsolutePath() }; ProcessBuilder builder = new ProcessBuilder(command); try { builder.start(); } catch (IOException e) { e.printStackTrace(); } context.startActivity(intent); } }); } @Override public void onDownloading(int progess) { final int progessCurrent=progess; ((Activity)context).runOnUiThread(new Runnable() { @Override public void run() { down_progress.setProgress(progessCurrent); } }); } @Override public void onDownloadFailed() { ((Activity)context).runOnUiThread(new Runnable() { @Override public void run() { ToastUtil.getLongToast(context,R.string.update_failed); } }); } });