diff --git a/README.md b/README.md index 9d0df76..cc86e78 100755 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Android APK 版本更新的下载和安装,适配7.0,8.0下载安装 #### 2.在Module目录下的build.gradle中添加依赖 ``` gradle dependencies { - compile 'com.github.maning0303:MNUpdateAPK:V2.0.0' + compile 'com.github.maning0303:MNUpdateAPK:V2.0.1' } ``` @@ -54,7 +54,7 @@ Android APK 版本更新的下载和安装,适配7.0,8.0下载安装 //下载APK InstallUtils.with(this) //必须-下载地址 - .setApkUrl(Constants.APK_URL_03) + .setApkUrl(Constants.APK_URL_01) //非必须-下载保存的文件的完整路径+/name.apk,使用自定义路径需要获取读写权限 .setApkPath(Constants.APK_SAVE_PATH) //非必须-下载回调 @@ -111,6 +111,8 @@ Android APK 版本更新的下载和安装,适配7.0,8.0下载安装 //取消下载 InstallUtils.cancleDownload(); + //是否正在下载 + InstallUtils.isDownloading(); //单独设置下载监听 InstallUtils.setDownloadCallBack(new InstallUtils.DownloadCallBack() { @@ -206,6 +208,11 @@ Android APK 版本更新的下载和安装,适配7.0,8.0下载安装 ##### 请添加okhttp3混淆 ## 版本记录: +##### 版本 V2.0.1: + 1.优化代码,防止部分手机出现异常情况 + 2.添加新的方法:isDownloading --- 判断是不是正在下载 + 3.优化onLoading回调频率,只有转progress+1才回调一次,防止在当前方法更新notify出现卡顿现象 + ##### 版本 V2.0.0: 1.升级下载,使用okhttp下载 2.优化安装代码,使用startActivityForResult() diff --git a/app/src/main/java/com/maning/mnupdateapk/MainActivity.java b/app/src/main/java/com/maning/mnupdateapk/MainActivity.java index a02b590..82de128 100755 --- a/app/src/main/java/com/maning/mnupdateapk/MainActivity.java +++ b/app/src/main/java/com/maning/mnupdateapk/MainActivity.java @@ -58,8 +58,10 @@ private void initViews() { @Override protected void onResume() { super.onResume(); - //设置监听 - InstallUtils.setDownloadCallBack(downloadCallBack); + //设置监听,防止其他页面设置回调后当前页面回调失效 + if (InstallUtils.isDownloading()) { + InstallUtils.setDownloadCallBack(downloadCallBack); + } } private void initCallBack() { @@ -97,8 +99,11 @@ public void onFail(Exception e) { @Override public void onLoading(long total, long current) { + //内部做了处理,onLoading 进度转回progress肯定是+1,防止频率过快 Log.i(TAG, "InstallUtils----onLoading:-----total:" + total + ",current:" + current); - tv_progress.setText((int) (current * 100 / total) + "%"); + int progress = (int) (current * 100 / total); + //进度要处理一下 + tv_progress.setText(progress + "%"); } @Override diff --git a/library_update/src/main/java/com/maning/updatelibrary/InstallUtils.java b/library_update/src/main/java/com/maning/updatelibrary/InstallUtils.java index 0868d95..e3314d8 100755 --- a/library_update/src/main/java/com/maning/updatelibrary/InstallUtils.java +++ b/library_update/src/main/java/com/maning/updatelibrary/InstallUtils.java @@ -30,6 +30,10 @@ public class InstallUtils { private String httpUrl; private String filePath; private static DownloadCallBack mDownloadCallBack; + /** + * 是不是在正在下载 + */ + private static boolean isDownloading = false; /** * 下载回调监听 @@ -52,13 +56,23 @@ public interface DownloadCallBack { private InstallUtils() { } + /** + * 是否正在下载 + */ + public static boolean isDownloading() { + return isDownloading; + } + /** * 设置监听 * * @param downloadCallBack */ public static void setDownloadCallBack(DownloadCallBack downloadCallBack) { - mDownloadCallBack = downloadCallBack; + //判断有没有开始 + if (isDownloading) { + mDownloadCallBack = downloadCallBack; + } } @@ -113,6 +127,10 @@ public InstallUtils setCallBack(DownloadCallBack downloadCallBack) { * 开始下载 */ public void startDownload() { + //先取消之前的下载 + if(isDownloading){ + cancleDownload(); + } //判断下载保存路径是不是空 if (TextUtils.isEmpty(filePath)) { filePath = MNUtils.getCachePath(mContext) + "/update.apk"; @@ -122,8 +140,11 @@ public void startDownload() { .url(httpUrl) .tag(InstallUtils.class) .execute(new AbsFileProgressCallback() { + int currentProgress = 0; + @Override public void onSuccess(String result) { + isDownloading = false; if (mDownloadCallBack != null) { mDownloadCallBack.onComplete(filePath); } @@ -131,13 +152,21 @@ public void onSuccess(String result) { @Override public void onProgress(long bytesRead, long contentLength, boolean done) { + isDownloading = true; if (mDownloadCallBack != null) { - mDownloadCallBack.onLoading(contentLength, bytesRead); + //计算进度 + int progress = (int) (bytesRead * 100 / contentLength); + //只有进度+1才回调,防止过快 + if (progress - currentProgress >= 1) { + mDownloadCallBack.onLoading(contentLength, bytesRead); + } + currentProgress = progress; } } @Override public void onFailed(String errorMsg) { + isDownloading = false; if (mDownloadCallBack != null) { mDownloadCallBack.onFail(new Exception(errorMsg)); } @@ -145,6 +174,7 @@ public void onFailed(String errorMsg) { @Override public void onStart() { + isDownloading = true; if (mDownloadCallBack != null) { mDownloadCallBack.onStart(); } @@ -152,6 +182,7 @@ public void onStart() { @Override public void onCancle() { + isDownloading = false; if (mDownloadCallBack != null) { mDownloadCallBack.cancle(); }