Main interface login automatically check for updates
checkupdateagent.addListener(checkUpDate); checkupdateagent.start();
Agent version Get service-side version information
Public class checkupdateagent extends abstractagent<String> { Public Static FinalString key_version ="Version"; Public Static FinalString Key_url ="url"; Public StaticStringdesc;Private intSleepmisec =4000; PublicCheckupdateagent () { This( the); } PublicCheckupdateagent (intSLEEPMISEC) { This. sleepmisec = sleepmisec; } @OverrideprotectedString Doexecute () throws Httprpcexception {Try{NewTimefairy (sleepmisec). Sleepifnecessary (); URL UpdateUrl =NewURL (Apputil.getupdateurl ()); HttpURLConnection conn = (httpurlconnection) updateurl.openconnection (); Properties props =NewProperties (); Props.load (Conn.getinputstream ()); String Version = Props.getproperty (key_version); String url=NULL;if(Apputil.shouldupdatetonewversion (version)) {url = props.getproperty (Key_url);desc=props.getproperty ("desc"); }returnUrl }Catch(Exception ex) {Throw NewHttprpcexception (Ex.getmessage (),0, ex); } }}
Tool Apputil, version judgment,
Public Static Boolean shouldupdatetonewversion(String serverversion) {string[] curversions = Versionname.split ("[.]"); string[] Serverversions = Serverversion.split ("[.]"); for(inti =0; I <3; i++) {intCurversion = Integer.parseint (Curversions[i]);intServversion = Integer.parseint (Serverversions[i]);if(Curversion > Servversion) {return false; }Else if(Curversion < Servversion) {return true; } }return false; }
Listen back
/** * GET Update Data monitoring * / PrivateAgentlistener<string> checkupdate =NewAgentlistener<string> () {@Override Public void onstarted(LongSEQ) {}@Override Public void onsucceeded(String info,LongSEQ) {if(info!=NULL) {Updatemanager =NewUpdatemanager (mainact. This, info); Showupdate (); } }@Override Public void onfailed(String errmsg,intErrcode,LongSEQ) {Viewutil.showtoast ("Update failed, please re-enable"); } };
Popup window Update or not
/** * Update Prompt box * / Private void showupdate() {Alertdialog.builder Builder =NewAlertdialog.builder (mainact. This); Builder.setmessage ("detected a new version, confirm the installation?" "); Builder.settitle ("Update Information"); Builder.setpositivebutton ("Install Now",NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {Dialog.dismiss (); Updatemanager.checkupdateinfo (); } }); Builder.setnegativebutton ("We'll talk later.",NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {Dialog.dismiss (); } }); Builder.show (); }
Download Updatemanager Download complete automatic installation
Public class updatemanager { / * Download Package Installation path * / Private Static FinalString Savepath ="/sdcard/yzxd/";Private Static FinalString Savefilename = Savepath +"yzxd.apk";PrivateContext Mcontext;//Return the installation package URL PrivateString apkurl="";PrivateDialog Downloaddialog;/ * progress bar with notification UI refreshed handler and MSG constants */ PrivateProgressBar mprogress;Private Static Final intDown_update =1;Private Static Final intDown_over =2;Private intProgressPrivateThread Downloadthread;Private BooleanInterceptflag =false;PrivateHandler Mhandler =NewHandler () { Public void Handlemessage(Message msg) {Switch(msg.what) { CaseDOWN_UPDATE:mProgress.setProgress (progress); Break; CaseDOWN_OVER:downloadDialog.dismiss (); INSTALLAPK (); Break;default: Break; } }; }; Public Updatemanager(context context, String Apkurl) { This. Mcontext = Context; This. Apkurl=apkurl; }//external interface Let main activity call Public void Checkupdateinfo() {showdownloaddialog (); }Private void Showdownloaddialog() {Alertdialog.builder Builder =NewAlertdialog.builder (Mcontext); Builder.settitle ("Downloading Updates");FinalLayoutinflater inflater = Layoutinflater.from (Mcontext); View v = inflater.inflate (r.layout.progress,NULL); Mprogress = (ProgressBar) V.findviewbyid (r.id.progress); Builder.setview (v); Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {Dialog.dismiss (); Interceptflag =true; } }); Downloaddialog = Builder.create (); Downloaddialog.show (); DOWNLOADAPK (); }PrivateRunnable mdownapkrunnable =NewRunnable () {@Override Public void Run() {Try{URL url =NewURL (Apkurl); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.connect ();intLength = Conn.getcontentlength (); InputStream is = Conn.getinputstream (); File File =NewFile (Savepath);if(!file.exists ()) {File.mkdir (); } String apkfile = Savefilename; File Apkfile =NewFile (Apkfile); FileOutputStream fos =NewFileOutputStream (Apkfile);intCount =0;byteBuf[] =New byte[1024x768]; do{intNumread = Is.read (BUF); Count + = Numread; Progress = (int)(((float) count/length) * -);//Update ProgressMhandler.sendemptymessage (down_update);if(Numread <=0){//Download complete notification installationMhandler.sendemptymessage (Down_over); Break; } fos.write (BUF,0, Numread); } while(!interceptflag);//Click Cancel to stop the download.Fos.close (); Is.close (); }Catch(Malformedurlexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); } } };/** * Download apk * @param * * Private void downloadapk() {Downloadthread =NewThread (mdownapkrunnable); Downloadthread.start (); }/** * Install apk * @param * * Private void installapk() {File apkfile =NewFile (Savefilename);if(!apkfile.exists ()) {return; } Intent i =NewIntent (Intent.action_view); I.setdataandtype (Uri.parse ("file://"+ apkfile.tostring ()),"Application/vnd.android.package-archive"); Mcontext.startactivity (i); }}
Automatic Version detection update