AIR Auto update introduction

AIR 1.5 加入了自动升级的类air.update.ApplicationUpdater,这样你的AIR应用程序便可以通过网络自动更新到最新版本了。

注意:Flex SDK 3.2以上才支持AIR 1.5。

自动更新的原理:ApplicationUpdater 会去读取网络上的一个XML版本描述文件,其中包含了版本号和对应的安装文件路径以及更新描述信息,然后和当前运行的AIR程序版本进行比较,来决定是否下载和安装。
这里有一个例子:

<?xml version="1.0" encoding="utf-8"?>  
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="checkForUpdate()">  
    <mx:Script>  
        <![CDATA[  
            import mx.controls.Alert;  
            import air.update.events.UpdateEvent;  
            import air.update.ApplicationUpdaterUI;  
            private var appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();  
  
            private function checkForUpdate():void {  
                appUpdater.updateURL = "http://localhost:8400/team/xml/test-update.xml"; // Server-side XML file describing update  
                appUpdater.isCheckForUpdateVisible = false; // We won't ask permission to check for an update  
                appUpdater.addEventListener(UpdateEvent.INITIALIZED, onUpdate); // Once initialized, run onUpdate  
                appUpdater.addEventListener(ErrorEvent.ERROR, onError); // If something goes wrong, run onError  
                appUpdater.initialize(); // Initialize the update framework  
            }    
            private function onError(event:ErrorEvent):void {  
                Alert.show(event.toString());  
            }  
            private function onUpdate(event:UpdateEvent):void {  
                appUpdater.checkNow(); // Go check for an update now  
            }  
        ]]>  
    </mx:Script>  
</mx:WindowedApplication>  

creationComplete=”checkForUpdate()”表示在程序加载完成后会连接到更新服务器上自动监测新版本。

服务器上的XML文件(test-update.xml)内容如下:

<?xml version="1.0" encoding="utf-8"?>   
<update xmlns="http://ns.adobe.com/air/framework/update/description/1.0">   
    <version>v1.1</version>   
    <url>http://localhost:8400/team/update/test.air</url>   
<description>update</description>   
</update>  

注意:XML中的版本号和指定的AIR程序的版本号一定要保证匹配。

请使用发行版本来测试,调试版本不支持升级安装。

当然,你可以可以采用第三方类包来简化开发流程:

下载everythingflex的类包: everythingflexairlib.swc 把类包copy到你工程的libs文件夹。

地址:http://everythingflexairlib.googlecode.com/files/everythingflexairlib.swc

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script> 
        <![CDATA[ 
            import com.everythingflex.air.managers.UpdateManager; 
            private var updateManager:UpdateManager = new UpdateManager("http://localhost:8400/team/test-update.xml",false); 
        ]]> 
    </mx:Script> 
    <mx:Button click="updateManager.checkForUpdate()" label="Test for Update" 
        horizontalCenter="0" verticalCenter="0"/> 
</mx:WindowedApplication> 

其中UpdateManager构造函数第2个参数代表是否自动检测新版本,TRUE为是,FALSE为否。

<?xml version="1.0" encoding="utf-8"?>
<currentVersion version="v1.1"
    downloadLocation="http://localhost:8400/team/update/test.air"
    forceUpdate="false" message="Added new features" />

同上,你还需要一个放在服务器上的XML文件,告知程序要更新到哪一个版本:

其中:downloadLocation是更新程序的路径,forceUpdate表示是否弹出警告框来提示用户更新程序,message是警告框中的详细信息。

开发过程可能遇到的错误代码列表:

16800 Occurs during validating the downloaded update file. The subErrorID may contain additional information.

16801 Invalid Adobe AIR file (missing application.xml).

16802 Invalid Adobe AIR file (missing mimetype).

16807 Invalid Adobe AIR file (format).

16804 Invalid Adobe AIR file(invalid flags).

16805 Invalid Adobe AIR file(unknown compression).

16806 Invalid Adobe AIR file (invalid filename).

16807 Invalid Adobe AIR file (corrupt).

16808 Configuration file does not exist.

16809 updateURL not set.

16810 Reserved.

16811 Invalid configuration file(unknown configuration version).

16812 Invalid configuration file (URL missing).

16813 Invalid configuration file (delay format).

16814 Invalid configuration file (invalid defaultUI values).

16815 Invalid update descriptor (unknown descriptor version).

16816 Invalid update descriptor (missing update version).

16817 Invalid update descriptor (invalid description).

16818 IO error while saving data to disk. subErrorID may provide more information.

16819 Security error while downloading. subErrorID may provide more information.

16820 Invalid HTTP status code. subErrorID may contain the invalid status code.

16821 Reserved.

16822 IO error while downloading. subErrorID may provide more information.

16823 EOF error while saving data to disk. subErrorID may provide more information.

16824 Invalid update descriptor. subErrorID may provide more information.

16825 The update file contains an application with a different application ID.

16826 The update file does not contain a newer version of the application.

16827 The version contained in the update file does not match the version from the update descriptor.

16828 Cannot update application, usually because the application is running in the AIR Debug Launcher (ADL).

16829 Missing update file at install time.

Move this post from: http://airfans.iteye.com/blog/528353

发表评论

电子邮件地址不会被公开。 必填项已用*标注