Android Online Update apk

Source: Internet
Author: User

1. Get information about the current package:

1 Packagemanager manager = Main.this. Getpackagemanager (); 2 try {3     packageinfo info = manager.getpackageinfo (Main.this.getPackageName (), 0); 4     String appversion = info.versionname; Version Name 5     currentversioncode = Info.versioncode;//Version number 6     System.out.println (Currentversioncode + "" + Appvers ION); 7} catch (Namenotfoundexception e) {8    }11//above is to get version data from manifest, using VERSIONCODE12// From the server to obtain the latest version of Versioncode, compared to Showupdatedialog ();     

2. Start the thread directly for download management and install:

1 Private voidShowupdatedialog () {2Alertdialog.builder Builder =NewAlertdialog.builder ( This);3Builder.settitle ("New version detected");4Builder.setmessage ("Do you want to download the update?"));5Builder.setpositivebutton ("Update",NewDialoginterface.onclicklistener () {6 7 @Override8              Public voidOnClick (Dialoginterface Dialog,intwhich) {9                 //TODO auto-generated Method Stub One Showdownloaddialog (); Dialog.dismiss (); -             } the}). Setnegativebutton ("Update Later",NewDialoginterface.onclicklistener () { -  - @Override -              Public voidOnClick (Dialoginterface Dialog,intwhich) { +                 //TODO auto-generated Method Stub -Dialog.dismiss (); +             } A         }); at builder.show (); -}

3. Pop up the download update progress box to download the APK update:

1 Private voidShowdownloaddialog () {2Alertdialog.builder Builder =NewBuilder (mcontext);3Builder.settitle ("Software version update");4         5         FinalLayoutinflater Inflater =Layoutinflater.from (mcontext);6View v = inflater.inflate (r.layout.progress,NULL);7Mprogress =(ProgressBar) V.findviewbyid (r.id.progress);8         9 Builder.setview (v);TenBuilder.setnegativebutton ("Cancel",NewOnclicklistener () { One @Override A              Public voidOnClick (Dialoginterface Dialog,intwhich) { - Dialog.dismiss (); -Interceptflag =true; the             } -         }); -Downloaddialog =builder.create (); - downloaddialog.show (); +          - downloadapk (); +}

4. Start the thread to download the task:

1  /** 2      * Download apk3      */ 4     5     Private void downloadapk () {6         New Thread (mdownapkrunnable); 7         Downloadthread.start (); 8     }

5. Control for download update Progress box: Interceptflag

1 PrivateRunnable mdownapkrunnable =NewRunnable () {2 @Override3          Public voidrun () {4             Try {5URL url =NewURL (apkurl);6             7HttpURLConnection conn =(HttpURLConnection) url.openconnection ();8 Conn.connect ();9                 intLength =conn.getcontentlength ();TenInputStream is =Conn.getinputstream (); One                  AFile File =NewFile (savepath); -                 if(!file.exists ()) { - File.mkdir (); the                 } -String Apkfile =Savefilename; -File Apkfile =NewFile (apkfile); -FileOutputStream fos =NewFileOutputStream (apkfile); +                  -                 intCount = 0; +                 byteBuf[] =New byte[1024]; A                  at                  Do{                       -                     intNumread =Is.read (BUF); -Count + =Numread; -Progress = (int)(((float) count/length) * 100); -                     //Update Progress - mhandler.sendemptymessage (down_update); in                     if(Numread <= 0){     -                         //Download complete notification installation to mhandler.sendemptymessage (down_over); +                          Break; -                     } theFos.write (buf,0, numread); *} while(!interceptflag);//Click Cancel to stop the download. $                 Panax Notoginseng fos.close (); - is.close (); the}Catch(malformedurlexception e) { + e.printstacktrace (); A}Catch(IOException e) { the e.printstacktrace (); +             } -              $         } $};

6. Update the progress bar of the prompt box:

1 PrivateHandler Mhandler =NewHandler () {2          Public voidhandlemessage (Message msg) {3             Switch(msg.what) {4              Casedown_update:5 mprogress.setprogress (progress);6                  Break;7              CaseDown_over:8                 9 installapk ();Ten                  Break; One             default: A                  Break; -             } -         }; the};

7. Download complete, APK installation:

1  /**2 * Install apk3      */4     Private voidinstallapk () {5File Apkfile =NewFile (savefilename);6         if(!apkfile.exists ()) {7             return;8         }    9Intent i =NewIntent (intent.action_view);TenI.setdataandtype (Uri.parse ("file://" + apkfile.tostring ()), "Application/vnd.android.package-archive");  One mcontext.startactivity (i); A      -}

Android Online Update apk

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.