要一颗不甜的糖 2023-06-12 17:16 采纳率: 0%
浏览 12

关于#android#的自动更新功能问题

我的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);
    }
}



  • 写回答

3条回答 默认 最新

  • 瞬间的未来式 2023-06-12 17:39
    关注

    安卓10以上好像有这个问题,可能和手机安全策略有关系

    评论

报告相同问题?

问题事件

  • 创建了问题 6月12日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表