app内下载apk修改。
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
package com.xscm.moduleutil.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.android.arouter.utils.TextUtils;
|
||||
import com.blankj.utilcode.util.FileUtils;
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
import com.blankj.utilcode.util.ThreadUtils;
|
||||
import com.blankj.utilcode.util.ToastUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -18,8 +23,12 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
@@ -42,18 +51,60 @@ public class DownloadUtil {
|
||||
private static final String PATH_AUDIO = Environment.getExternalStorageDirectory() + "/qipao/audio";
|
||||
|
||||
|
||||
public DownloadUtil(Context context,String downUrl) {
|
||||
this.mContext = context;
|
||||
|
||||
if (mApi == null) {
|
||||
//初始化网络请求接口
|
||||
mApi = ApiHelper.getInstance().createService(ApiInterface.class);
|
||||
int i = downUrl.lastIndexOf('/');//一定是找最后一个'/'出现的位置
|
||||
if (i != -1) {
|
||||
downUrl = downUrl.substring(i);
|
||||
}
|
||||
mApkPath = new File(Objects.requireNonNull(mContext.getExternalFilesDir(PATH_APK+"_"+downUrl)).getAbsolutePath()).getAbsolutePath();
|
||||
@SuppressLint("CheckResult")
|
||||
public DownloadUtil(Context context, String downUrl) {
|
||||
// 1. 判空,避免空指针
|
||||
if (context == null) {
|
||||
ToastUtils.showShort("Context 不能为 null!");
|
||||
LogUtils.e("Context 不能为 null!."+(mApi == null));
|
||||
return;
|
||||
}
|
||||
if (downUrl == null || downUrl.trim().isEmpty()) {
|
||||
ToastUtils.showShort("下载链接不能为 null 或空!");
|
||||
LogUtils.e("下载链接不能为 null 或空!."+(mApi == null));
|
||||
return;
|
||||
}
|
||||
|
||||
this.mContext = context;
|
||||
final String dUrl = downUrl;
|
||||
File qipaoDir = new File(mContext.getFilesDir(), "qipao");
|
||||
if (qipaoDir.exists() && qipaoDir.isDirectory()) {
|
||||
Observable.create(new ObservableOnSubscribe<Boolean>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
|
||||
LogUtils.e("删除文件中."+(mApi == null));
|
||||
FileUtils.deleteAllInDir(qipaoDir);
|
||||
emitter.onNext(true);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
|
||||
LogUtils.e("删除成功."+(mApi == null));
|
||||
if (mApi == null) {
|
||||
//初始化网络请求接口
|
||||
mApi = ApiHelper.getInstance().createService(ApiInterface.class);
|
||||
int i = dUrl.lastIndexOf('/');//一定是找最后一个'/'出现的位置
|
||||
String downUrlEnd = "";
|
||||
if (i != -1) {
|
||||
downUrlEnd = dUrl.substring(i);
|
||||
if (downUrlEnd.contains("/")) {
|
||||
downUrlEnd = downUrlEnd.replace("/", "");
|
||||
}
|
||||
}
|
||||
mApkPath = new File(mContext.getFilesDir(), PATH_APK + downUrlEnd).getAbsolutePath();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (mApi == null) {
|
||||
//初始化网络请求接口
|
||||
mApi = ApiHelper.getInstance().createService(ApiInterface.class);
|
||||
int i = downUrl.lastIndexOf('/');//一定是找最后一个'/'出现的位置
|
||||
if (i != -1) {
|
||||
downUrl = downUrl.substring(i);
|
||||
}
|
||||
mApkPath = new File(mContext.getFilesDir(), PATH_APK + downUrl.replace("/", "")).getAbsolutePath();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void downloadVoiceFile(String url, final DownloadListener downloadListener) {
|
||||
|
||||
Reference in New Issue
Block a user