Skip to content

Commit

Permalink
代码优化,修复bug,v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Sep 5, 2018
1 parent c60a7d0 commit 68a908f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand All @@ -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)
//非必须-下载回调
Expand Down Expand Up @@ -111,6 +111,8 @@ Android APK 版本更新的下载和安装,适配7.0,8.0下载安装
//取消下载
InstallUtils.cancleDownload();

//是否正在下载
InstallUtils.isDownloading();

//单独设置下载监听
InstallUtils.setDownloadCallBack(new InstallUtils.DownloadCallBack() {
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/maning/mnupdateapk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class InstallUtils {
private String httpUrl;
private String filePath;
private static DownloadCallBack mDownloadCallBack;
/**
* 是不是在正在下载
*/
private static boolean isDownloading = false;

/**
* 下载回调监听
Expand All @@ -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;
}
}


Expand Down Expand Up @@ -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";
Expand All @@ -122,36 +140,49 @@ 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);
}
}

@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));
}
}

@Override
public void onStart() {
isDownloading = true;
if (mDownloadCallBack != null) {
mDownloadCallBack.onStart();
}
}

@Override
public void onCancle() {
isDownloading = false;
if (mDownloadCallBack != null) {
mDownloadCallBack.cancle();
}
Expand Down

0 comments on commit 68a908f

Please sign in to comment.