AIR应用更新

准备工作:update.xml,xxx.air两个文件存放在服务器update目录下

update.xml

<update xmlns="http://ns.adobe.com/air/framework/update/description/2.5">
<versionNumber>0.0.1</versionNumber>
<url>http://localhost:8080/update/xxx.air</url>
<description/>
</update>

 

检查更新

     //读取配置文件
        public function ReadConfig():void
        {
            //获取该版本号
            appUpdater=new ApplicationUpdater(); 
            Global.getInstance().currentVersion = appUpdater.currentVersion;    
var urlLoad:URLLoader=new URLLoader();
            urlLoad.load(new URLRequest("http://localhost:8080/update/update.xml"));
            urlLoad.addEventListener(HTTPStatusEvent.HTTP_STATUS,CheckFunction);
       
        }
        
        protected function CheckFunction(event:HTTPStatusEvent):void
        {
            if(event.status == 200)
            {
                checkVersion();
            }
            else
            {
                this.dispatchEvent(new Event(Event.COMPLETE));
          //如果网络不通,就跳过更新 } }
//检查版本自动更新 protected function checkVersion():void { beetleApp.startPage.Tips="正在检测版本更新……"; NativeApplication.nativeApplication.addEventListener( Event.EXITING, function(e:Event):void { var opened:Array = NativeApplication.nativeApplication.openedWindows; for (var i:int = 0; i < opened.length; i ++) { opened[i].close(); } }); //区别就是是否有提示 //appUpdater = new ApplicationUpdater(); appUpdater.isNewerVersionFunction=isNewerVersionFunction; //appUpdater.configurationFile = configurationFile; //appUpdater.isCheckForUpdateVisible = false; appUpdater.updateURL = Constants.UPDATE_URL; appUpdater.addEventListener(UpdateEvent.INITIALIZED, updaterInitialized); appUpdater.addEventListener(StatusUpdateEvent.UPDATE_STATUS, statusUpdate); appUpdater.addEventListener(UpdateEvent.BEFORE_INSTALL, beforeInstall); appUpdater.addEventListener(StatusUpdateErrorEvent.UPDATE_ERROR, statusUpdateError); appUpdater.addEventListener(UpdateEvent.DOWNLOAD_START, downloadStarted); appUpdater.addEventListener(ProgressEvent.PROGRESS, downloadProgress); appUpdater.addEventListener(UpdateEvent.DOWNLOAD_COMPLETE, downloadComplete); appUpdater.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR, downloadError); appUpdater.addEventListener(ErrorEvent.ERROR, updaterError); appUpdater.initialize(); } private var appVersion:String; private var baseURL:String; private var upateVersion:String; private var description:String; private var isFirstRun:String; private var applicationName:String; private var installedVersion:String; private function updaterInitialized(event:UpdateEvent):void { this.isFirstRun = event.target.isFirstRun; this.applicationName = getApplicationName(); this.installedVersion = getApplicationVersion(); appUpdater.checkNow(); } private function getApplicationVersion():String { var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor; var ns:Namespace = appXML.namespace(); return appXML.ns::version; } /** * Getter method to get the name of the application, this does not support multi-language. * Based on a method from Adobes ApplicationUpdaterDialogs.mxml, which is part of Adobes AIR Updater Framework * Also based on Jens Krause blog post: http://www.websector.de/blog/2009/09/09/custom-applicationupdaterui-for-using-air-updater-framework-in-flex-4/ * * @return String name of application * */ private function getApplicationName():String { var applicationName:String; var xmlNS:Namespace=new Namespace("http://www.w3.org/XML/1998/namespace"); var appXML:XML=NativeApplication.nativeApplication.applicationDescriptor; var ns:Namespace=appXML.namespace(); // filename is mandatory var elem:XMLList=appXML.ns::filename; // use name is if it exists in the application descriptor if ((appXML.ns::name).length() != 0) { elem=appXML.ns::name; } // See if element contains simple content if (elem.hasSimpleContent()) { applicationName=elem.toString(); } return applicationName; } private function statusUpdate(event:StatusUpdateEvent):void { // stop the event from triggering the update download/install by itself trace(event.toString()); event.preventDefault(); if (event.available) { var alertInfo:String="发现新版本(" + event.version + ") ,是否立即升级?"; var alertTitle:String="升级提示"; var alertFlag:uint=Alert.OK|Alert.CANCEL; Alert.okLabel = "是"; Alert.cancelLabel = "下次再说"; Alert.show(alertInfo,alertTitle,alertFlag,this,updateChoiceAlertHandler); }else { this.dispatchEvent(new Event(Event.COMPLETE)); } } private function updateChoiceAlertHandler(event:CloseEvent) : void { trace("updateChoiceAlertHandler: "+event.toString()); if (event.detail == Alert.OK) { appUpdater.downloadUpdate(); trace("Alert.OK"); }else{ appUpdater.cancelUpdate(); this.dispatchEvent(new Event(Event.COMPLETE)); trace("Alert.cancel"); } } private function beforeInstall(event:UpdateEvent):void { trace(event); } private function statusUpdateError(event:StatusUpdateErrorEvent):void { event.preventDefault(); } private function downloadStarted(event:UpdateEvent):void { trace(event); } private function downloadProgress(event:ProgressEvent):void { var num:int = (event.bytesLoaded/event.bytesTotal)*100; trace("已更新软件 "+num+"%"); } private function downloadComplete(event:UpdateEvent):void { event.preventDefault(); // prevent default install } private function downloadError(event:DownloadErrorEvent):void { event.preventDefault(); trace(event.text); } private function updaterError(event:ErrorEvent):void { trace(event.text); } protected function isNewerVersionFunction(currentVersion:String, updateVersion:String):Boolean { var local_vers:Array = currentVersion.split('.'); var remote_vers:Array = updateVersion.split('.'); var local_ver:int; var remote_ver:int; var local_points:Number = 0; var remote_points:Number = 0; for (var i:int=0; i<Math.max(local_vers.length, remote_vers.length); i++) { local_ver = local_vers.length <= i ? 0 : int(local_vers[i]); remote_ver = remote_vers.length <= i ? 0 : int(remote_vers[i]); local_points = local_points * 100 + local_ver; remote_points = remote_points * 100 + remote_ver; } return remote_points > local_points; }

 

posted @ 2016-07-19 22:17  天才混混  阅读(234)  评论(0编辑  收藏  举报