Android APK online Upgrade

Source: Internet
Author: User

Now Android development, generally adopt CS mode, then APK upgrade, naturally need to have server side support. In general, we put the upgraded version and a configuration file that records the upgraded version (in the Jsonarray format in this article) on the server side. When client initializes, if the server side is detected with an updated version (read the configuration file of the server), the upgrade version on the server side is connected in HTTP, downloaded, and then called the Android API to replace the upgrade.

One, the configuration file:

Update_version.json

[{"appname":"myapp","apkname" :"myapp.apk","vername":1.0,"  vercode":1}]

The version comparison is based on the version number defined in the Vercode in Update_version.json and the Androidmanifest.xml file in the apk file:

Android:versioncode="1"android:versionname="1.0" 

Second, the version of the comparison when initializing

//Compare Server versions//called in the OnCreate functionif(Getserververcode ()) {intVercode = Config.getvercode ( This); if(Newvercode >Vercode) {donewversionupdate ();//new version updates found}Else{Toast.maketext (Getapplicationcontext (),"currently the latest version, thanks for your support", Toast.length_long). Show ();//no new version    }}//child function, get version number PrivateBoolean Getserververcode () {Try {        //get server address and interface file nameString Verjson =networktool.getcontent (Config.update_server+Config.update_verjson); Jsonarray Array=NewJsonarray (Verjson); if(Array.Length () >0) {Jsonobject obj= Array.getjsonobject (0); Try{Newvercode= Integer.parseint (Obj.getstring ("Vercode")); Newvername= Obj.getstring ("Vername"); } Catch(Exception e) {Newvercode= -1; Newvername=""; return false; }        }    } Catch(Exception e) {log.e (TAG, E.getmessage ()); return false; }    return true;} //child function, if there is a new version, the Private voiddonewversionupdate () {intVercode = Config.getvercode ( This); String Vername= Config.getvername ( This); StringBuffer SB=NewStringBuffer (); Sb.append ("Current Version:");    Sb.append (Vername); Sb.append ("Code:");    Sb.append (Vercode); Sb.append (", new versions found");    Sb.append (Newvername); Sb.append ("Code:");    Sb.append (Newvercode); Sb.append (", is it updated?"); Dialog Dialog=NewAlertdialog.builder (updateactivity. This). Settitle ("Software Updates"). Setmessage (Sb.tostring ())//Set Content. Setpositivebutton ("Update",//Set OK button                    NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface dialog,intwhich) {PBar=NewProgressDialog (updateactivity. This); Pbar.settitle ("is downloading"); Pbar.setmessage ("Please wait ...");                            Pbar.setprogressstyle (Progressdialog.style_spinner); Downfile (Config.update_server+config.update_apkname); }}). Setnegativebutton ("not updated at this stage",                    NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface dialog,intWhichbutton) {                            //exit the program after clicking the "Cancel" buttonfinish (); }}). Create ();//Create//Show dialog Boxdialog.show ();}

Third, download and upgrade

23456789Ten One A - - the - - - + - + A at - - - - - in - to + - the * $Panax Notoginseng - the + A the + - $ $ - - the -Wuyi the - Wu - About $ - - - A + the - $voiddownfile (final String URL) {pbar.show (); NewThread () { Public voidrun () {HttpClient client=Newdefaulthttpclient (); HttpGetGet=Newhttpget (URL);            HttpResponse response; Try{Response= Client.execute (Get); Httpentity Entity=response.getentity (); LongLength =entity.getcontentlength (); InputStream is=entity.getcontent (); FileOutputStream FileOutputStream=NULL; if( is!=NULL) {File file=NewFile (Environment.getexternalstoragedirectory (), Config.update_                    Savename); FileOutputStream=Newfileoutputstream (file); byte[] buf =New byte[1024x768]; intCH =-1; intCount =0;  while(ch = is. Read (BUF))! =-1) {fileoutputstream.write (buf,0, CH); Count+=ch; if(Length >0) {}}} Fileoutputstream.flush (); if(FileOutputStream! =NULL) {fileoutputstream.close ();            } down (); } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }}}.start (); } voidDown () {Handler.post (NewRunnable () { Public voidrun () {pbar.cancel ();        Update (); }    }); } voidUpdate () {Intent Intent=NewIntent (Intent.action_view); Intent.setdataandtype (Uri.fromfile (NewFile (Environment. getExternalStorageDirectory (), config.update_savename)),"application/vnd.android.package-archive"); StartActivity (intent);}

Reference: http://www.cnblogs.com/Amandaliu/archive/2011/08/22/2148936.html

Android APK online Upgrade

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.