我的android自动更新功能出现了问题,我点击更新后,每次就下载了几kb内容,下载不完全,然后就会导致我安装包解析失败,有没有专家帮忙看看
package com.example.accounting.updateManager;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;
import com.example.accounting.BuildConfig;
import java.io.File;
import java.util.List;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.update.AppVersion;
public class UpdateManager {
private Context mContext;
private String mApkUrl;
public UpdateManager(Context context, String apkUrl) {
mContext = context;
mApkUrl = apkUrl;
}
public void checkUpdate() {
int currentVersionCode = BuildConfig.VERSION_CODE;
BmobQuery<AppVersion> query = new BmobQuery<>();
query.findObjects(new FindListener<AppVersion>() {
@Override
public void done(List<AppVersion> list, BmobException e) {
if (e != null) {
Log.e("UpdateManager", "查询版本信息失败:" + e.getMessage());
return;
}
if (list == null || list.isEmpty()) {
Log.e("UpdateManager", "服务器上没有版本信息");
return;
}
double latestVersionCode = list.get(0).getVersion_i();
if (latestVersionCode > currentVersionCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("发现新版本");
builder.setMessage(list.get(0).getUpdate_log());
builder.setPositiveButton("更新", (dialog, which) -> downloadLatestVersion());
builder.setNegativeButton("取消", null);
builder.show();
}
}
});
}
private void downloadLatestVersion() {
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(mApkUrl));
downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
downloadRequest.setTitle("天天记账");
downloadRequest.setDescription("正在下载最新版本的APP");
downloadRequest.setDestinationInExternalFilesDir(mContext, null, "day_day_bookkeeping.apk"); // 修改了这一行
downloadRequest.allowScanningByMediaScanner();
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadRequest.setMimeType("application/vnd.android.package-archive");
DownloadManager downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = downloadManager.enqueue(downloadRequest);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id == downloadId) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
installApk();
} else {
Log.d("下载失败", "状态: " + status + ", 原因: " + reason);
}
}
cursor.close();
}
}
};
mContext.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
private void installApk() {
File apkFile = new File(mContext.getExternalFilesDir(null), "day_day_bookkeeping.apk");
Uri apkUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", apkFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (apkFile.exists()) {
Log.d("存在","");
//文件存在
} else {
Log.d("不存在","");
//文件不存在
}
mContext.startActivity(intent);
}
}