Phonegap Android应用程序以编程方式触发更新

Ver*_*ero 14 android auto-update cordova

我正在构建一个托管在Google Play外部服务器上的Android应用.我需要应用程序检查新版本并提示用户在应用程序启动时进行更新.

我构建了一个机制来检查新版本(应用程序检查服务器上的文件并比较版本),该机制运行良好.然后我提示用户更新,但如果用户选择继续更新,我无法触发apk的下载和安装.

我试过打开apk url:

window.open(apkURL);
Run Code Online (Sandbox Code Playgroud)

其中apkURL是服务器上托管的.apk文件的完整http链接.

但它似乎没有做任何事情.

我一直在研究,但我找不到如何做到这一点的答案.我发现了一些使用本机代码的建议,比如这篇文章在Android上以编程方式安装应用程序,但我不知道如何在我的Phonegap应用程序中执行此操作.

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall); 
Run Code Online (Sandbox Code Playgroud)

这是触发更新的正确方法吗?如何在Phonegap应用程序中完成这样的事情?

谢谢!

Ver*_*ero 16

我终于使用File api和WebIntent插件设法实现了这一点.我会在这里发布解决方案,以防它帮助任何人.

代码将apk从远程服务器下载到SD卡中的下载文件夹,然后触发安装.

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile('download/filename.apk', {
    create: true, 
    exclusive: false
  }, function(fileEntry) {
    var localPath = fileEntry.fullPath,
    fileTransfer = new FileTransfer();        
    fileTransfer.download(apkURL, localPath, function(entry) {
        window.plugins.webintent.startActivity({
            action: window.plugins.webintent.ACTION_VIEW,
            url: 'file://' + entry.fullPath,
            type: 'application/vnd.android.package-archive'
            },
            function(){},
            function(e){
                alert('Error launching app update');
            }
        );                              

    }, function (error) {
        alert("Error downloading APK: " + error.code);
  });
  }, function(evt){
      alert("Error downloading apk: " + evt.target.error.code);                                               
  });
}, function(evt){
alert("Error preparing to download apk: " + evt.target.error.code);
});
Run Code Online (Sandbox Code Playgroud)


Rob*_*tto 12

对于使用Cordova 3+的人来说,可以使用与Vero类似的解决方案:

所以我们首先要下载.apk文件.我们需要文件传输插件.您可以使用以下命令安装它:

phonegap local plugin add org.apache.cordova.file-transfer
Run Code Online (Sandbox Code Playgroud)

其次,我们需要另一个插件来启动webintent.此webintent会提示用户是否有更新.您可以使用以下命令安装它:

phonegap local plugin add https://github.com/Initsogar/cordova-webintent.git
Run Code Online (Sandbox Code Playgroud)

然后,您可以在代码中使用这两个函数来下载.apk,并在有应用程序更新时提示用户:

/*
 * Uses the filetransfer plugin
 */
function downloadApkAndroid(data) {
    var fileURL = "cdvfile://localhost/persistent/CegekaMon.apk";

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

    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,
        {

        }
    );
}
/*
 * Uses the borismus webintent plugin
 */
function promptForUpdateAndroid(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);
        }
    );
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答.downloadApkAndroid函数中的'data'参数是什么?你可以分享一个叫它的例子吗? (2认同)