特别注意: 两个更新前后的apk,必须保证签名和包名完全一致,才可以覆盖安装,否则安装会失败!!!
工具类代码如下:
1.下载更新APK的工具
public class InstallUtils {
//任务定时器
private Timer mTimer;
//定时任务
private TimerTask mTask;
//文件总大小
private int fileLength = 1;
//下载的文件大小
private int fileCurrentLength;
private Context context;
private String httpUrl;
private String savePath;
private String saveName;
private DownloadCallBack downloadCallBack;
private static File saveFile;
private boolean isComplete = false;
public interface DownloadCallBack {
void onStart();
void onComplete(String path);
void onLoading(long total, long current);
void onFail(Exception e);
}
public interface InstallCallBack {
void onSuccess();
void onFail(Exception e);
}
public InstallUtils(Context context, String httpUrl, String saveName, DownloadCallBack downloadCallBack) {
this.context = context;
this.httpUrl = httpUrl;
this.saveName = saveName;
this.downloadCallBack = downloadCallBack;
this.savePath = getCachePath(this.context);
}
public void downloadAPK() {
if (TextUtils.isEmpty(httpUrl)) {
return;
}
saveFile = new File(savePath);
if (!saveFile.exists()) {
boolean isMK = saveFile.mkdirs();
if (!isMK) {
//创建失败
return;
}
}
saveFile = new File(savePath + File.separator + saveName + ".apk");
if (downloadCallBack != null) {
//下载开始
downloadCallBack.onStart();
}
new Thread(new Runnable() {
@Override
public