Steps to implement Automatic Updates for Android software

Source: Internet
Author: User



This article is a direct download of the latest APK installation method, not an incremental download of the APK.

Reprint Please specify source: http://blog.csdn.net/harryweasley/article/details/44955719, thank you

To implement an Android app that automatically updates the method of downloading the APK software, I take the following steps:

1. Each time you enter the main interface, get the server data to see if it is the latest version, yes, then no action, no, the following steps;

2. Popup whether to update the Software dialog box, click on the download

3. Pop up the Download progress Bar dialog box, start the download, you can click on the button at any time to stop the download

4. After the download is complete, call the system installation software service, install the software

:







Implementation process:



Create a new Updatemanager method, I already have a detailed comment

Package Lgx.acc.updatedemo;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream;import Java.net.httpurlconnection;import Java.net.url;import Android.app.alertdialog;import Android.content.Context; Import Android.content.dialoginterface;import Android.content.intent;import Android.net.uri;import Android.os.handler;import Android.view.layoutinflater;import Android.view.view;import Android.widget.ProgressBar; Import Android.widget.textview;public class Updatemanager {//Application contextprivate Context mcontext;//is the latest app, The default is falseprivate boolean isnew = false;private Boolean intercept = false;//Download the network path for the installation package private String Apkurl = "Http://shouj i.360tpcdn.com/360sj/tpi/20130201/"+" com.flikie.wallpapers.gallery_4.apk ";//Save the APK folder private static final String Savepath = "/sdcard/updatedemo/";p rivate static final String savefilename = savepath+ "updatedemorelease.apk";// Download thread private thread Downloadthread;private int progress;//Current Progress TextView text;//progress bar with notification UI refresh handler and MSG constants PRIVate ProgressBar mprogress;private static final int down_update = 1;private static final int down_over = 2;public Updatema Nager (Context context) {Mcontext = context;} /** * Check if the content is updated */public void Checkupdateinfo () {//The isnew here is originally to be obtained from the server, I'm here to assume that he needs to update if (isnew) {return;} else { Showupdatedialog ();}} /** * Displays the Updater dialog box for the main program to call */private void Showupdatedialog () {Alertdialog.builder Builder = new Alertdialog.builder (mcontext ); Builder.settitle ("Software version update"); Builder.setmessage ("There is the latest package, please download!"); Builder.setpositivebutton ("Download", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {showdownloaddialog ();}}); Builder.setnegativebutton ("Again later", New Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show (); /** * Show Download progress dialog box */private void Showdownloaddialog () {Alertdialog.builder Builder = new Alertdialog.builder (mcontext); Builder.settitle ("software version update"); Layoutinflater 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", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {intercept = true;}}); Builder.show ();d ownloadapk ();} /** * Download APK install package from server */private void downloadapk () {downloadthread = new Thread (mdownapkrunnable);d Ownloadthread.start ();} Private Runnable mdownapkrunnable = new Runnable () {@Overridepublic void run () {URL url;try {url = new URL (apkurl); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.connect (); int length = Conn.getcontentlength () ; InputStream ins = Conn.getinputstream (); File File = new file (Savepath), if (!file.exists ()) {File.mkdir ();} File Apkfile = new file (savefilename); FileOutputStream fos = new FileOutputStream (apkfile); int count = 0;byte[] buf = new Byte[1024];while (!intercept) {int num Read = Ins.read (BUF); count + = Numread;progrESS = (int) ((float) count/length) * 100);//Download Progress mhandler.sendemptymessage (down_update); if (numread <= 0) {//Download complete notification Install Mhandler.sendemptymessage (down_over); Fos.write (buf, 0, numread);} Fos.close (); Ins.close ();} catch (Exception e) {e.printstacktrace ();}}};/ * * Install apk content */private void installapk () {File apkfile = new File (savefilename); if (!apkfile.exists ()) {return;} Intent Intent = new Intent (Intent.action_view); Intent.setdataandtype (Uri.parse ("file://" + apkfile.tostring ()), " Application/vnd.android.package-archive "); mcontext.startactivity (intent);}; Private Handler Mhandler = new Handler () {public void Handlemessage (android.os.Message msg) {switch (msg.what) {case Down_ UPDATE:mProgress.setProgress (progress); break;case down_over:installapk (); break;default:break;}};}


and the specific code for Progressbar.xml.


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <progressbar        android:id= "@+id/progress"        style= "? android:attr/ Progressbarstylehorizontal "        android:layout_width=" fill_parent "        android:layout_height=" wrap_content "/ >  </LinearLayout>



Then in Mainactivity's OnCreate method, call the code to

Updatemanager manager=new Updatemanager (mainactivity.this); Manager.checkupdateinfo ();



Be sure to remember to add permission to the manifest.

<uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>



Finally, the effect comes out.


Do you have any suggestions please advise ~ ~ ~


Steps to implement Automatic Updates for Android software

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.