1.引入第三方库
https://github.com/WVector/AppUpdate
2.使用
public static void onlyDownload(final String mApkFileUrl, final Activity context) {
// Log.d(TAG, "onlyDownload() called with: view = [" + view + "]");
UpdateAppBean updateAppBean = new UpdateAppBean();
String apkurl =mApkFileUrl;
if (!apkurl.startsWith("http")){
apkurl = "http://lbb.ty-eco.com/picture"+mApkFileUrl;
}
//设置 apk 的下载地址
updateAppBean.setApkFileUrl(apkurl);
String path = "";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {
try {
path = context.getExternalCacheDir().getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(path)) {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
} else {
path = context.getCacheDir().getAbsolutePath();
}
//设置apk 的保存路径
updateAppBean.setTargetPath(path);
//实现网络接口,只实现下载就可以
updateAppBean.setHttpManager(new UpdateAppHttpUtil());
HProgressDialogUtils.showHorizontalProgressDialog(context, "下载进度", false);
UpdateAppManager.download(context, updateAppBean, new DownloadService.DownloadCallback() {
@Override
public void onStart() {
Log.d(TAG, "onStart() called");
}
@Override
public void onProgress(float progress, long totalSize) {
HProgressDialogUtils.setProgress(Math.round(progress));
Log.d(TAG, "onProgress() called with: progress = [" + progress + "], totalSize = [" + totalSize + "]");
}
@Override
public void setMax(long totalSize) {
Log.d(TAG, "setMax() called with: totalSize = [" + totalSize + "]");
}
@Override
public boolean onFinish(File file) {
HProgressDialogUtils.cancel();
Log.d(TAG, "onFinish() called with: file = [" + file.getAbsolutePath() + "]");
return true;
}
@Override
public void onError(String msg) {
HProgressDialogUtils.cancel();
Log.e(TAG, "onError() called with: msg = [" + msg + "]");
}
@Override
public boolean onInstallAppAndAppOnForeground(File file) {
Log.d(TAG, "onInstallAppAndAppOnForeground() called with: file = [" + file + "]");
return false;
}
});
}