0

我正在尝试实现自动更新功能以在常规市场之外分发应用程序。我混合使用了 Cordova 插件来下载和执行我的应用程序的更新版本。

我可以看到我将 apk 下载到外部存储,启动它,允许安装,但没有任何反应(它回到我原来的应用程序)。 apk更新步骤

我感觉这是一个权限问题,但我在日志中看不到任何内容。 如果我从文件系统打开下载的 apk,我可以毫无问题地安装它。 仅当我从 cordova 应用程序启动 apk 时,安装不起作用。

这是我用来进行更新的代码:

/**
 * Begin the download and install of the update for android.
 */
update() {
    let _this = this;
    let update = this._updateResult.get();

    // Check permissions first
    let permissions = cordova.plugins.permissions;
    let list = [permissions.WRITE_EXTERNAL_STORAGE];

    let errorPermissions = () => {
        sAlert.error("Cannot update permissions");
    };

    let successPermissions = () => {
        let fileTransfer = new FileTransfer();

        // let targetLocation = cordova.file.externalDataDirectory + "app.apk"; // better use externalCacheDirectory
        let targetLocation = "file:///storage/emulated/0/Download/"+"app-1.4.1.apk";
        console.debug("Begin update in appManager ", update);
        console.info("Download file", update.apk);
        console.info("Download to ", targetLocation);

        let onSuccess = (entry) => {
            let fileURL = entry.toURL();
            console.debug("download complete!", fileURL);

            cordova.plugins.fileOpener2.open(
                fileURL,
                'application/vnd.android.package-archive',
                {
                    error: function (e) {
                        console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                        _this._updating.set(false);
                    },
                    success: function () {
                        console.log('file opened successfully');
                        _this._updating.set(false);
                    }
                }
            );
        }
        let onError = (error) => {
            _this._updating.set(false);
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("download error code" + error.code);
        }
        let options = {
            chunkedMode: true,
            mimeType: "application/vnd.android.package-archive"
        };

        fileTransfer.download(
            encodeURI(update.apk),
            targetLocation,
            onSuccess,
            onError,
            options,
            true // trustAllHosts
        );
        fileTransfer.onprogress = (progressEvent) => {
            if (progressEvent.lengthComputable) {
                let percent = Math.floor((progressEvent.loaded / progressEvent.total) * 100);
                _this._updating.set(percent);
            } else {
                _this._updating.set(true);
            }
        };
    }

    permissions.hasPermission(list,
        function (status) {
            console.debug("permissions status is", status);
            if (status.hasPermission) {
                successPermissions();
            } else {
                permissions.requestPermissions(
                    list,
                    function (status) {
                        if (!status.hasPermission) {
                            errorPermissions();
                        }
                        successPermissions();
                    },
                    errorPermissions
                );
            }
        }, errorPermissions);
}

我还尝试添加其他权限,例如 INSTALL_PACKAGES、DELETE_PACKAGES、RESTART_PACKAGES...

我想代码的唯一重要部分应该是

cordova.plugins.fileOpener2.open(
                fileURL,
                'application/vnd.android.package-archive',
                {
                    error: function (e) {
                        console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                        _this._updating.set(false);
                    },
                    success: function () {
                        console.log('file opened successfully');
                        _this._updating.set(false);
                    }
                }
            );

我的日志显示“文件打开成功”,非常感谢您的帮助:)

4

1 回答 1

1

检查fileopener2插件的代码后,发现intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 被注释掉了,我尝试添加标志,现在更新有效。

于 2017-09-01T05:43:21.743 回答