2013-04-08 52 views
14

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

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

我试图简单地打开APK网址:

window.open(apkURL); 

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

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

我一直在研究,但没有找到如何做到这一点的答案。我发现了一些使用本机代码的建议,像这篇文章Install Application programmatically on 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); 

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

谢谢!

回答

15

我终于设法实现了这个,使用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); 
}); 
+1

安装后使用phonegap apk如何自动启动apk?是否有任何特定的方法在fileapi或webintent插件请让我知道 – nida 2013-10-22 04:54:58

+1

我想从http://download.com/update/myapp.apk下载更新。那么代码是什么?请帮忙。 – 2015-08-06 04:45:19

0

不幸的是,你不能在不使用插件的情况下访问Phonegap web容器中的那种本地特性,你所能做的就是将用户链接到apk上(通过打开本机浏览器)并让他安装它。

+0

谢谢你的回答。我正在检查[webintent](https://github.com/phonegap/phonegap-plugins/tree/master/Android/WebIntent)phonegap插件,看它是否可以工作。当你说到apk的链接,它是一个正常的链接?为什么链接工作,但不是我的window.open?谢谢。 – Vero 2013-04-08 21:48:36

+0

当然使用插件你可以绕过科尔多瓦的限制,我不知道这个插件,但我认为它应该工作。通过链接,我实际上想打开本机浏览器来打开apk文件,如navigator.app.loadUrl。 – zakinster 2013-04-09 07:45:53

11

对于你们来说,用科尔多瓦3+,像维拉的一个类似的解决方案是可能的:

因此,我们首先要下载.apk文件。我们需要这个文件传输插件。 你可以用下面的命令来安装它:

phonegap local plugin add org.apache.cordova.file-transfer 

其次,我们需要另一个插件启动webintent。如果有更新,此webintent会提示用户。你可以用下面的命令来安装它:

phonegap local plugin add https://github.com/Initsogar/cordova-webintent.git 

然后,您可以使用此功能2在你的代码下载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); 
     } 
    ); 
} 
+2

感谢您的回答。 downloadApkAndroid函数中的'data'参数是什么?你能分享一个例子吗? – carlospiles 2014-11-20 10:55:07

+0

只是一个字符串数组,'data.android'包含要下载的APK的URL。 – janpio 2015-11-17 04:14:26

+0

如何调用该功能? downloadApkAndroid({机器人:“/files/app.apk”}) – 2015-12-27 19:57:00

0

谢谢你Vero的回答...这对我很有帮助... 你的代码在Cordova文件插件的最后一个版本中存在问题。 如果使用文件插件版本1.0.0或更新版本,请使用entry.toURL()替换entry.fullPath。 如果你使用entry.fullPath,它会抛出错误'有问题解析包'。

from file-transfer plugin on github

这些路径在FileEntry的和的DirectoryEntry的FULLPATH属性以前暴露的对象由文件插件返回。然而,File插件的新版本不再将这些路径公开给JavaScript。

如果您正在升级到File的新版本(1.0.0或更新版本),并且您以前一直在使用entry.fullPath作为参数来下载()或上传(),那么您将需要更改代码改为使用文件系统URL。

FileEntry.toURL()和DirectoryEntry.toURL()返回的形式的文件系统URL

cdvfile://本地主机/持久性/路径/到可以代替绝对文件的使用/文件 下载()和上传()方法的路径。

相关问题