Smart Phone Online Update program

Source: Internet
Author: User
Recently completed an online update of something. Because the smartphone app doesn't have updates like ClickOnce, write one yourself. (In fact, ClickOnce is not easy to use, to write a more convenient themselves). Update the background with Webservice,httphandler, here is not much to say, mainly talk about the foreground design. The update is divided into 4 steps: 1. Download server version and update list, compare the local version, whether need to update; 2. Download the updated version of the server; 3. Overwrite local version; 4. Restart the main application;  if you use the traditional design, 4 steps are simple, define an enumeration corresponding to each step;///<summary>    ///System automatic upgrade steps     ///</summary>     public enum Autoupdatestep     {        ///<summary>        ///getting server version         ///</summary>         seeingserveredition = 1,        ///<summary>        /// Downloading server-side files        ///</summary>          Downloadingserverfiles = 2,        ///<summary>         ///server file saved        ///</summary>          serverfilesdownloaded = 3,        ///<summary>        ///updated        ///</summary>         finished = 4,        ///<summary >        ///unknown State        ///</summary >         unknow = 0} then If...else ... Determine the steps and perform the appropriate steps. But it's going to be a hassle if you want to insert a step in the middle. After consideration, if you introduce a command mode, it is convenient to implement each step. Define the base class for each step: Public abstract class UpdateCommand     {        protected context _context;           public UpdateCommand (context)          {            This._context = context;        }            #region Abstract Method         public abstract void Execute ();         #endregion}  Get server-side version and update list: public class Getserverversioncommand:updatecommand {public Getserverversioncommand (context Co ntext): Base (context) {} public override void Execute () {/ /Achieve a little ...} }
Download server-side new version: public class Downloaddatacommand:updatecommand     {      & nbsp Public Downloaddatacommand            : Base (context)         {          }           public override void Execute ()          {           //implementation slightly ...         } 2 Other steps to update the local version, complete the update restart main program similar, Savedownloaddatacommand, Finishdownloadcommand. The context is used to interact with the main interface of the middle layer, a bit of the taste of intermediaries, mainly defined a series of events for the implementation and the main interface interaction. This can achieve the main interface and the various commands of the solution of the Lotus. public sealed class smartupdatemanager      {        #region Properties         private list<updatecommand> Commands = null;         private int Index = 0;           Private context _context = null;         Public Context Updatecontext         {            Get              {                 if (this._context = null)                  {                     _context = new Context ();                }                  return this._context;           &nbsP }        }           #endregion           public Smartupdatemanager ()         {            inithandleupdate ();         }           private void inithandleupdate ()          {            Commands = new List <UpdateCommand> ();             Getserverversioncommand cmd1 = new Getserverversioncommand (Updatecontext);             Downloaddatacommand cmd2 = new Downloaddatacommand (Updatecontext);             Savedownloaddatacommand cmd3 = new Savedownloaddatacommand(Updatecontext);             Finishdownloadcommand CMD4 = new Finishdownloadcommand (Updatecontext);               Commands.add (CMD1);             commands.add (CMD2);             Commands.add (CMD3);             Commands.add (CMD4);        }           #region public method         public void Execute ()         {               Innerhandexecute ();        }           private void Innerhandexecute ()          {            try              {                  if (this. Index < this. Commands.count)                 {                      this. Commands[this. Index]. Execute ();                      this. index++;                }             }              catch (Exception)             {            }        }          #endregion  } Customer interface calls, very convenient, the main interface subscription Smartupdatemanager events in the context of the update progress bar, prompting information and other interactive information. Private Smartupdatemanager manager = null; private void Initupdatemanager ()         {             manager = new Smartupdatemanager ();             Manager. Updatecontext.applicationexit + = new Applicationexithandler (manager_onapplicationexit);             Manager. Updatecontext.enabledchanged + = new Enabledchangedhandler (manager_onenabledchanged);             Manager. Updatecontext.processbarvaluechanged + = new Progressarvaluechangedhandler (manager_onprocessbarvaluechanged);             Manager. Updatecontext.promptmsgchanged + = new Promptmsgchangedhandler (manager_onpromptmsgchanged);             Manager. Updatecontext.textchanged + = new Textchangedhandler (manager_ontextchanged);             Manager. Updatecontext.visiablechanged + = new Visiablechangedhandler (manager_onvisiablechanged);             Manager. Updatecontext.workingtipmessagechanged + = new Workingtipmessagechangedhandler (manager_onworkingtipmessagechanged) ;             Manager. Updatecontext.cancelenablechanged + = new Cancelenablechangedhandler (manager_oncancelenabled);                    }   void Btnnext_click (object sender, EventArgs e)         {&NBsp;           Try              {                 This.psbDownload.Value = 0;                 this.btnNext.Enabled = false;                 This.manager.Execute ();                // Application.doevents ();            }              catch (Exception)             {                //.........            } &NBsp;      &nbsp}       Such a design is easy to implement the wizard, and I actually implement the wizard-style updates. Prompts the user step-by-step. If you want to add a link in the middle, such as adding a welcome link, a proofing document link. Then we just need to change the code in the Smartupdatemanager. We have added 2 Welcomecommand, Validatecommand. private void Inithandleupdate ()         {             Commands = new list<updatecommand> ();            //_context = new Context ();               Getserverversioncommand cmd1 = new Getserverversioncommand (Updatecontext);             Downloadda

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.