对于使用 Cordova 3+ 的用户,类似 Vero 的解决方案是可行的:
首先,我们需要下载 .apk 文件。我们需要使用 file-transfer 插件来完成此操作。
您可以使用以下命令进行安装:
phonegap local plugin add org.apache.cordova.file-transfer
其次,我们需要另一个插件来启动WebIntent。这个WebIntent会提示用户是否需要更新。您可以使用以下命令进行安装:
phonegap local plugin add https://github.com/Initsogar/cordova-webintent.git
然后,您可以在您的代码中使用这两个函数来下载.apk文件和提示用户应用程序是否有更新:
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,
{
}
);
}
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);
}
);
}