编程方式Phonegap Android应用触发器更新 [英] Phonegap Android app trigger update programmatically
问题描述
我正在建立一个由Google Play以外的伺服器代管的Android应用程式。我需要应用程序检查新版本,并提示用户更新应用程序启动时。
I am building an Android app that is hosted on a server outside Google Play. I need the app to check for new version and prompt user to update when the app starts.
我建立了一个机制来检查新版本(应用程序检查文件在服务器和比较版本),这是工作很好。然后我提示用户更新,但如果用户选择继续更新,我无法触发下载和安装apk。
I built a mechanism to check for new version (the app checks for a file on server and compares version) which is working well. Then I prompt user to update, but if user chooses to proceed with the update, I'm not able to trigger the download and installation of the apk.
我试过简单地打开apk网址:
I tried simply opening the apk url:
window.open(apkURL);
其中apkURL是到服务器上承载的.apk文件的完整http链接。
where the apkURL is the full http link to the .apk file hosted on server.
但它似乎没有做任何事情。
But it doesn't seem to do anything.
我一直在研究,但我没有找到答案如何做到这一点。我使用原生代码找到了一些建议,例如在Android上以编程方式安装应用,但我不知道如何做到这一点从我的Phonegap应用程序。
I've been researching but I don't find an answer on how to do this. I found some suggestions using native code, like this post Install Application programmatically on Android but I don't know how to do that from within my Phonegap app.
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
这是触发更新的正确方法吗?如何从这样的东西可以在Phonegap应用程序内完成?
Is this the right way to trigger the update? How can something like this be done from within a Phonegap app?
谢谢!
推荐答案
文件 api和 WebIntent 插件。我将在这里发布解决方案,以防它帮助任何人。
I finally managed to implement this, using the File api and the WebIntent plugin. I will post the solution here in case it helps anyone.
代码将apk从远程服务器下载到sdcard中的下载文件夹,然后触发安装。 / p>
The code downloads the apk from a remote server to the download folder in the sdcard and then triggers the install.
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);
});
这篇关于编程方式Phonegap Android应用触发器更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!