android cordova app以编程方式触发更新

时间:2016-11-14 06:31:42

标签: android cordova auto-update

我使用此代码从我的localhost下载apk

function downloadApkAndroid(data) {
var fileURL = "cdvfile://localhost/persistent/path/to/Download/";

var fileTransfer = new FileTransfer();
var uri = encodeURI(data);

fileTransfer.download(
    uri,
    fileURL,
    function (entry) {

        console.log("download complete: " + entry.fullPath);

        promptForUpdateAndroid(entry);
    },
    function (error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {

    }
);

}

我成功下载然后它将调用此函数promptForUpdateAndroid(entry);尝试安装函数包含此函数

function promptForUpdateAndroid(entry) {
console.log(entry);
window.plugins.webintent.startActivity({
        action: window.plugins.webintent.ACTION_VIEW,
        url: entry.toURL(),
        type: 'application/vnd.android.package-archive'
    },
    function () {
    },
    function () {
        alert('Failed to open URL via Android Intent.');
        console.log("Failed to open URL via Android Intent. URL: " + entry.fullPath);
    }
);

}

在调用该功能后我得到警告

  

解析错误 - 解析包

时出现问题

我认为fileURL var fileURL = "cdvfile://localhost/persistent/path/to/Download/";是错误的,但我真的不知道路径到底是什么。请帮帮我

1 个答案:

答案 0 :(得分:0)

好吧我已经解决了我的问题。我放在这里,因为有人遇到同样的问题。它的真实,我放的路径实际上是错误的,因为我得到了apk我下载表单服务器,但无法安装它因为它没有权限,因为它puth在root用户所以我改变路径

var fileURL = cordova.file.externalDataDirectory+'android.apk'

然后在成功下载文件后,web intent将自动提示用户安装与否。